airshipctl/pkg/phase/printers.go
Pallav Gupta 10e75b5580 Fix plan list command
The aim of this PS to fix current issue we have when we have
new plan created with description width more than 40 char.
Added condition if width is more than specified in command
so the command execution will not break and just crop
exceeding description length.
Eg: e351b2bc43/pkg/print/table/columndefs.go (L71)

Change-Id: Icc353ec688b2fbb563add39f8d38004008093895
2021-04-15 12:18:23 -07:00

104 lines
2.7 KiB
Go

/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package phase
import (
"errors"
"fmt"
"io"
"k8s.io/apimachinery/pkg/runtime"
"opendev.org/airship/airshipctl/pkg/api/v1alpha1"
"opendev.org/airship/airshipctl/pkg/util"
"sigs.k8s.io/cli-utils/pkg/print/table"
)
//PrintPhaseListTable prints phase list table
func PrintPhaseListTable(w io.Writer, phases []*v1alpha1.Phase) error {
rt, err := util.NewResourceTable(phases, util.DefaultStatusFunction())
if err != nil {
return err
}
printer := util.DefaultTablePrinter(w, nil)
clusternameCol := table.ColumnDef{
ColumnName: "clustername",
ColumnHeader: "CLUSTER NAME",
ColumnWidth: 20,
PrintResourceFunc: func(w io.Writer, width int, r table.Resource) (int,
error) {
phase, err := phaseFromResource(r)
if err != nil {
return 0, nil
}
txt := phase.ClusterName
if len(txt) > width {
txt = txt[:width]
}
_, err = fmt.Fprintf(w, txt)
return len(txt), err
},
}
executorrefkindCol := table.ColumnDef{
ColumnName: "executorrefkind",
ColumnHeader: "EXECUTOR",
ColumnWidth: 20,
PrintResourceFunc: func(w io.Writer, width int, r table.Resource) (int,
error) {
phase, err := phaseFromResource(r)
if err != nil {
return 0, nil
}
txt := phase.Config.ExecutorRef.Kind
if len(txt) > width {
txt = txt[:width]
}
_, err = fmt.Fprintf(w, txt)
return len(txt), err
},
}
docentrypointCol := table.ColumnDef{
ColumnName: "docentrypoint",
ColumnHeader: "DOC ENTRYPOINT",
ColumnWidth: 100,
PrintResourceFunc: func(w io.Writer, width int, r table.Resource) (int,
error) {
phase, err := phaseFromResource(r)
if err != nil {
return 0, nil
}
txt := phase.Config.DocumentEntryPoint
if len(txt) > width {
txt = txt[:width]
}
_, err = fmt.Fprintf(w, txt)
return len(txt), err
},
}
printer.Columns = append(printer.Columns, clusternameCol, executorrefkindCol, docentrypointCol)
printer.PrintTable(rt, 0)
return nil
}
func phaseFromResource(r table.Resource) (*v1alpha1.Phase, error) {
rs := r.ResourceStatus()
if rs == nil {
return nil, errors.New("Resource status is nil")
}
phase := &v1alpha1.Phase{}
return phase, runtime.DefaultUnstructuredConverter.FromUnstructured(rs.Resource.Object, phase)
}