github.com/jenkins-x/jx/v2@v2.1.155/pkg/cmd/step/get/step_get_dependency_version.go (about) 1 package get 2 3 import ( 4 "fmt" 5 "strings" 6 7 "github.com/jenkins-x/jx/v2/pkg/cmd/helper" 8 "github.com/jenkins-x/jx/v2/pkg/cmd/opts" 9 "github.com/jenkins-x/jx/v2/pkg/cmd/opts/step" 10 "github.com/jenkins-x/jx/v2/pkg/cmd/templates" 11 "github.com/jenkins-x/jx/v2/pkg/dependencymatrix" 12 "github.com/pkg/errors" 13 "github.com/spf13/cobra" 14 ) 15 16 var ( 17 getDependencyVersionLong = templates.LongDesc(` 18 Outputs the version of a specific dependency from the dependency matrix in the version stream or a local directory 19 `) 20 21 getDependencyVersionExample = templates.Examples(` 22 # display the version of jx in the version stream 23 jx step get dependency-version --host=github.com --owner=jenkins-x --repo=jx 24 25 # display the version of jx in a local directory containing a "dependency-matrix" subdirectory, only logging the version 26 jx step get dependency-version --host=github.com --owner=jenkins-x --repo=jx --dir=/some/directory --short 27 `) 28 ) 29 30 // StepGetDependencyVersionOptions contains the command line flags 31 type StepGetDependencyVersionOptions struct { 32 step.StepOptions 33 34 Host string 35 Owner string 36 Repo string 37 Dir string 38 ShortOutput bool 39 } 40 41 // NewCmdStepGetDependencyVersion Creates a new Command object 42 func NewCmdStepGetDependencyVersion(commonOpts *opts.CommonOptions) *cobra.Command { 43 options := &StepGetDependencyVersionOptions{ 44 StepOptions: step.StepOptions{ 45 CommonOptions: commonOpts, 46 }, 47 } 48 49 cmd := &cobra.Command{ 50 Use: "dependency-version", 51 Short: "Outputs the version of a dependency from the Jenkins X dependency matrix", 52 Long: getDependencyVersionLong, 53 Example: getDependencyVersionExample, 54 Run: func(cmd *cobra.Command, args []string) { 55 options.Cmd = cmd 56 options.Args = args 57 err := options.Run() 58 helper.CheckErr(err) 59 }, 60 } 61 cmd.Flags().StringVarP(&options.Host, "host", "", "", "Host for dependency repo in the matrix") 62 cmd.Flags().StringVarP(&options.Owner, "owner", "", "", "Owner for dependency repo in the matrix") 63 cmd.Flags().StringVarP(&options.Repo, "repo", "", "", "Repo name for dependency repo in the matrix") 64 cmd.Flags().StringVarP(&options.Dir, "dir", "", "", "Directory to read dependency matrix from instead of using the version stream") 65 cmd.Flags().BoolVarP(&options.ShortOutput, "short", "", false, "Display the dependency version only") 66 return cmd 67 } 68 69 // Run implements this command 70 func (o *StepGetDependencyVersionOptions) Run() error { 71 var missingArgs []string 72 73 if o.Host == "" { 74 missingArgs = append(missingArgs, "host") 75 } 76 if o.Owner == "" { 77 missingArgs = append(missingArgs, "owner") 78 } 79 if o.Repo == "" { 80 missingArgs = append(missingArgs, "repo") 81 } 82 if len(missingArgs) > 0 { 83 return fmt.Errorf("one or more required arguments are missing: %s", strings.Join(missingArgs, ", ")) 84 } 85 86 if o.Dir == "" { 87 resolver, err := o.GetVersionResolver() 88 if err != nil { 89 return err 90 } 91 o.Dir = resolver.VersionsDir 92 } 93 94 matrix, err := dependencymatrix.LoadDependencyMatrix(o.Dir) 95 if err != nil { 96 return errors.Wrapf(err, "failed to load dependency matrix at %s", o.Dir) 97 } 98 99 version, err := matrix.FindVersionForDependency(o.Host, o.Owner, o.Repo) 100 if err != nil { 101 return err 102 } 103 if o.ShortOutput { 104 fmt.Fprintf(o.Out, "%s\n", version) 105 return nil 106 } 107 fmt.Fprintf(o.Out, "Version for host %s, owner %s, repo %s in matrix at %s is: %s\n", o.Host, o.Owner, o.Repo, o.Dir, version) 108 return nil 109 }