github.com/jlmeeker/kismatic@v1.10.1-0.20180612190640-57f9005a1f1a/pkg/cli/ip.go (about)

     1  package cli
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"io"
     7  
     8  	"github.com/apprenda/kismatic/pkg/install"
     9  	"github.com/spf13/cobra"
    10  )
    11  
    12  type ipOpts struct {
    13  	planFilename string
    14  }
    15  
    16  // NewCmdIP prints the cluster's IP
    17  func NewCmdIP(out io.Writer) *cobra.Command {
    18  	opts := &ipOpts{}
    19  
    20  	cmd := &cobra.Command{
    21  		Use:   "ip",
    22  		Short: "retrieve the IP address of the cluster",
    23  		RunE: func(cmd *cobra.Command, args []string) error {
    24  			if len(args) != 0 {
    25  				return fmt.Errorf("Unexpected args: %v", args)
    26  			}
    27  			planner := &install.FilePlanner{File: opts.planFilename}
    28  			return doIP(out, planner, opts)
    29  		},
    30  	}
    31  
    32  	// PersistentFlags
    33  	cmd.PersistentFlags().StringVarP(&opts.planFilename, "plan-file", "f", "kismatic-cluster.yaml", "path to the installation plan file")
    34  
    35  	return cmd
    36  }
    37  
    38  func doIP(out io.Writer, planner install.Planner, opts *ipOpts) error {
    39  	// Check if plan file exists
    40  	if !planner.PlanExists() {
    41  		return planFileNotFoundErr{filename: opts.planFilename}
    42  	}
    43  	plan, err := planner.Read()
    44  	if err != nil {
    45  		return fmt.Errorf("error reading plan file: %v", err)
    46  	}
    47  	address, err := getClusterAddress(*plan)
    48  	if err != nil {
    49  		return err
    50  	}
    51  	fmt.Fprintln(out, address)
    52  	return nil
    53  }
    54  
    55  func getClusterAddress(plan install.Plan) (string, error) {
    56  	if plan.Master.LoadBalancedFQDN == "" {
    57  		return "", errors.New("Master load balanced FQDN is not set in the plan file")
    58  	}
    59  	return plan.Master.LoadBalancedFQDN, nil
    60  }