github.skymusic.top/operator-framework/operator-sdk@v0.8.2/cmd/operator-sdk/printdeps/cmd.go (about)

     1  // Copyright 2018 The Operator-SDK Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package printdeps
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"github.com/operator-framework/operator-sdk/internal/pkg/scaffold"
    21  	"github.com/operator-framework/operator-sdk/internal/pkg/scaffold/ansible"
    22  	"github.com/operator-framework/operator-sdk/internal/pkg/scaffold/helm"
    23  	"github.com/operator-framework/operator-sdk/internal/util/projutil"
    24  
    25  	"github.com/spf13/cobra"
    26  )
    27  
    28  var asFile bool
    29  
    30  func NewCmd() *cobra.Command {
    31  	printDepsCmd := &cobra.Command{
    32  		Use:   "print-deps",
    33  		Short: "Print Golang packages and versions required to run the operator",
    34  		Long: `The operator-sdk print-deps command prints all Golang packages and versions expected
    35  by this version of the Operator SDK. Versions for these packages should match
    36  those in an operators' go.mod or Gopkg.toml file, depending on the dependency
    37  manager chosen when initializing or migrating a project.
    38  
    39  print-deps prints in columnar format by default. Use the --as-file flag to
    40  print in go.mod or Gopkg.toml file format.
    41  `,
    42  		RunE: printDepsFunc,
    43  	}
    44  
    45  	printDepsCmd.Flags().BoolVar(&asFile, "as-file", false, "Print dependencies in go.mod or Gopkg.toml file format, depending on the dependency manager chosen when initializing or migrating a project")
    46  
    47  	return printDepsCmd
    48  }
    49  
    50  func printDepsFunc(cmd *cobra.Command, args []string) error {
    51  	if len(args) != 0 {
    52  		return fmt.Errorf("command %s doesn't accept any arguments", cmd.CommandPath())
    53  	}
    54  
    55  	if err := printDeps(asFile); err != nil {
    56  		return fmt.Errorf("print deps failed: (%v)", err)
    57  	}
    58  	return nil
    59  }
    60  
    61  func printDeps(asFile bool) error {
    62  	// Make sure the project has a dep manager file.
    63  	mt, err := projutil.GetDepManagerType()
    64  	if err != nil {
    65  		return err
    66  	}
    67  	isDep := mt == projutil.DepManagerDep
    68  
    69  	// Migrated Ansible and Helm projects will be of type OperatorTypeGo but
    70  	// their deps files will differ from a vanilla Go project.
    71  	switch {
    72  	case projutil.IsOperatorAnsible():
    73  		if isDep {
    74  			return ansible.PrintDepGopkgTOML(asFile)
    75  		}
    76  		return ansible.PrintGoMod(asFile)
    77  	case projutil.IsOperatorHelm():
    78  		if isDep {
    79  			return helm.PrintDepGopkgTOML(asFile)
    80  		}
    81  		return helm.PrintGoMod(asFile)
    82  	case projutil.IsOperatorGo():
    83  		if isDep {
    84  			return scaffold.PrintDepGopkgTOML(asFile)
    85  		}
    86  		return scaffold.PrintGoMod(asFile)
    87  	}
    88  
    89  	return projutil.ErrUnknownOperatorType{}
    90  }