airshipctl/pkg/phase/printers.go
Niharika Bhavaraju 9bf40366a5 [#358] Introduce Phase List command to output phase list(pkg module).
* Phase list command lists phases of current documentset/plan in
  table or yaml format.
 airshipctl phase list
 airshipctl phase list --plan planName
 airshipctl phase list --plan planName -o yaml
 airshipctl phase list --plan planName -o table(default format)

Relates-To: #358

Co-Authored-By: Niharika Bhavaraju <niha.twinkle@gmail.com>

Change-Id: I37add2fc9dca2433de525bac8c2cc9e56fe39621
2021-03-25 20:16:27 +05:30

89 lines
2.4 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
}
return fmt.Fprintf(w, phase.ClusterName)
},
}
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
}
return fmt.Fprintf(w, phase.Config.ExecutorRef.Kind)
},
}
docentrypointCol := table.ColumnDef{
ColumnName: "docentrypoint",
ColumnHeader: "DOC ENTRYPOINT",
ColumnWidth: 40,
PrintResourceFunc: func(w io.Writer, width int, r table.Resource) (int,
error) {
phase, err := phaseFromResource(r)
if err != nil {
return 0, nil
}
return fmt.Fprintf(w, phase.Config.DocumentEntryPoint)
},
}
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)
}