Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

(GH-176) Add default validation group #185

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions cmd/initialize/initialize.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion cmd/validate/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"github.com/puppetlabs/prm/cmd/initialize"
"github.com/rs/zerolog/log"
"net/http"
"os"
Expand Down Expand Up @@ -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))

Expand Down
3 changes: 0 additions & 3 deletions pkg/prm/prm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down