diff --git a/cmd/initialize/initialize.go b/cmd/initialize/initialize.go new file mode 100644 index 0000000..b985719 --- /dev/null +++ b/cmd/initialize/initialize.go @@ -0,0 +1,85 @@ +package initialize + +import ( + "fmt" + "os" + "path/filepath" + + "github.com/rs/zerolog/log" + + "github.com/puppetlabs/prm/pkg/prm" + "github.com/spf13/cobra" +) + +var ( + format string + prmApi *prm.Prm +) + +func CreateCommand(parent *prm.Prm) *cobra.Command { + prmApi = parent + + tmp := &cobra.Command{ + Use: "initialize", + Short: "Initiates a directory with a `validate.yml` file", + Long: "Initiates a directory with a `validate.yml` file, for multi-tool validation", + PreRunE: preExecute, + RunE: execute, + } + + tmp.Flags().SortFlags = false + tmp.Flags().StringVarP(&format, "format", "f", "human", "display output in human or json format") + err := tmp.RegisterFlagCompletionFunc("format", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + return []string{"human", "json"}, cobra.ShellCompDirectiveNoFileComp + }) + cobra.CheckErr(err) + + return tmp +} + +func preExecute(cmd *cobra.Command, args []string) error { + switch prmApi.RunningConfig.Backend { + default: + prmApi.Backend = &prm.Docker{AFS: prmApi.AFS, IOFS: prmApi.IOFS, ContextTimeout: prmApi.RunningConfig.Timeout} + } + return nil +} + +func execute(cmd *cobra.Command, args []string) error { + wd, err := os.Getwd() + if err != nil { + return err + } + + filePath := filepath.Join(wd, "validate.yml") + + _, err = prmApi.AFS.Stat(filePath) + if err == nil { + return fmt.Errorf("content has already been initialized") + } + + file, err := prmApi.AFS.Create(filePath) + if err != nil { + return err + } + + groups := `groups: + - id: default + tools: + - name: puppetlabs/epp + - name: puppetlabs/puppet-syntax + - name: puppetlabs/metadata-json-lint +` + + _, err = file.WriteString(groups) + if err != nil { + return err + } + + if err := file.Close(); err != nil { + log.Error().Msgf("Error closing file: %s", err) + } + + log.Info().Msgf("PRM content initialized successfully") + return nil +} diff --git a/cmd/validate/validate.go b/cmd/validate/validate.go index 81433d4..bc960f8 100644 --- a/cmd/validate/validate.go +++ b/cmd/validate/validate.go @@ -276,7 +276,7 @@ func execute(cmd *cobra.Command, args []string) error { } if isSerial && cmd.Flags().Changed("workerCount") { - log.Warn().Msgf("The --workerCount flag has no affect when used with the --serial flag") + log.Info().Msgf("The --workerCount flag has no affect when used with the --serial flag") } if isSerial || workerCount < 1 { workerCount = 1 diff --git a/main.go b/main.go index 886039e..379bc1a 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "context" + "github.com/puppetlabs/prm/cmd/initialize" "github.com/rs/zerolog/log" "net/http" "os" @@ -77,6 +78,9 @@ func main() { // validate command rootCmd.AddCommand(validate.CreateCommand(prmApi)) + // validate command + rootCmd.AddCommand(initialize.CreateCommand(prmApi)) + // status command rootCmd.AddCommand(status.CreateStatusCommand(prmApi)) diff --git a/pkg/prm/prm.go b/pkg/prm/prm.go index f7c107d..9cb2139 100644 --- a/pkg/prm/prm.go +++ b/pkg/prm/prm.go @@ -87,9 +87,6 @@ func checkDuplicateToolsInGroups(tools []ToolInst) error { func getSelectedGroup(groups []Group, selectedGroupID string) (Group, error) { if selectedGroupID == "" && len(groups) > 0 { - if selectedGroupID == "" { - log.Warn().Msgf("No group specified. Defaulting to the '%s' tool group", groups[0].ID) - } selectedGroupID = groups[0].ID }