Skip to content

Commit

Permalink
Merge pull request #34 from pquerna/pq/config_path
Browse files Browse the repository at this point in the history
Configuration Path as an Arg
  • Loading branch information
valllabh authored May 30, 2024
2 parents 59dc7c9 + d63363d commit b27696e
Show file tree
Hide file tree
Showing 11 changed files with 190 additions and 132 deletions.
15 changes: 10 additions & 5 deletions commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@ import (
// Define the root command
var rootCmd = &cobra.Command{
Use: "ocsf-tool",

PostRun: func(cmd *cobra.Command, args []string) {
// Write the config file to disk
config.WriteConfig()
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
configFile, err := cmd.Flags().GetString("config")
if err != nil {
return err
}
if err := config.InitConfig(configFile); err != nil {
return err
}
return nil
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
println("--")
Expand All @@ -20,7 +25,7 @@ var rootCmd = &cobra.Command{

// Initialize the root command
func init() {

rootCmd.PersistentFlags().String("config", "config.yaml", "config file path")
}

func GetRootCmd() *cobra.Command {
Expand Down
5 changes: 5 additions & 0 deletions commands/schema_class_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package commands

import (
"encoding/json"
"sort"

"github.com/spf13/cobra"
"github.com/valllabh/ocsf-tool/commons"
Expand Down Expand Up @@ -40,6 +41,10 @@ func runSchemaClassListCmd(cmd *cobra.Command, args []string) {
classesByCategory[class.Category] = append(classesByCategory[class.Category], class.Name)
}

for _, classes := range classesByCategory {
sort.Strings(classes)
}

if outputFilePath != "" {
contents, _ := json.Marshal(classesByCategory)
// Write classes by Category to output file
Expand Down
4 changes: 2 additions & 2 deletions commons/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package commons
import (
"os"

"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
)

func GitCloneRepository(url, directory string) error {
Expand Down
31 changes: 4 additions & 27 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,17 @@ import (
"github.com/spf13/viper"
)

func getConfigFilePath() string {
return "config.yaml"
}

// initConfig reads in config file and ENV variables if set.
func InitConfig() {

viper.SetConfigName("config")
func InitConfig(configFile string) error {
viper.SetConfigType("yaml")
viper.AddConfigPath(".")

// Read in environment variables that match
err := viper.ReadInConfig()

// Handle errors reading the config file
if err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
// Config file not found; create it with default values
viper.WriteConfigAs(getConfigFilePath()) // handle error
} else {
// Config file was found but another error was produced
panic(fmt.Errorf("fatal error config file: %s", err))
}
}
viper.SetConfigFile(configFile)

return viper.ReadInConfig()
}

// Write config file to disk
func WriteConfig() {
err := viper.WriteConfigAs(getConfigFilePath())
err := viper.WriteConfigAs("config.yaml")

// Handle errors writing the config file
if err != nil {
Expand All @@ -44,7 +25,3 @@ func WriteConfig() {

println("Config saved.")
}

func LoadConfig() {

}
26 changes: 17 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,55 @@ module github.com/valllabh/ocsf-tool
go 1.21.0

require (
github.com/go-git/go-git/v5 v5.12.0
github.com/hashicorp/go-multierror v1.1.1
github.com/iancoleman/strcase v0.3.0
github.com/jinzhu/copier v0.4.0
github.com/spf13/afero v1.11.0
github.com/spf13/cobra v1.8.0
github.com/spf13/viper v1.18.2
gopkg.in/src-d/go-git.v4 v4.13.1
)

require (
dario.cat/mergo v1.0.0 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/ProtonMail/go-crypto v1.0.0 // indirect
github.com/cloudflare/circl v1.3.7 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.3 // indirect
github.com/emirpasic/gods v1.12.0 // indirect
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
github.com/go-git/go-billy/v5 v5.5.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/kevinburke/ssh_config v0.0.0-20190725054713-01f96b0aa0cd // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/pjbgf/sha1cd v0.3.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sergi/go-diff v1.0.0 // indirect
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect
github.com/skeema/knownhosts v1.2.2 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/src-d/gcfg v1.4.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/xanzy/ssh-agent v0.2.1 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/mod v0.13.0 // indirect
golang.org/x/net v0.23.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
golang.org/x/tools v0.14.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/src-d/go-billy.v4 v4.3.2 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
)

Expand Down
Loading

0 comments on commit b27696e

Please sign in to comment.