Skip to content

Commit

Permalink
cleaning code
Browse files Browse the repository at this point in the history
  • Loading branch information
Slimo300 committed Mar 24, 2024
1 parent 11bf5c8 commit d36c2af
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 41 deletions.
1 change: 1 addition & 0 deletions cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,6 @@ func init() {

return nil
})

deleteCmd.Flags().StringSliceVar(&conditions, "condition", []string{}, "condition for target objects to fulfill")
}
11 changes: 1 addition & 10 deletions cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,6 @@ func init() {

return nil
})
getCmd.Flags().StringSliceVar(&conditions, "condition", []string{}, "condition for target objects to fulfill")

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// getCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
getCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
getCmd.Flags().StringSliceVar(&conditions, "condition", []string{}, "condition for target objects to fulfill")
}
9 changes: 0 additions & 9 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,7 @@ func Execute() {
}

func init() {
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.

rootCmd.AddCommand(getCmd)
rootCmd.AddCommand(setCmd)
rootCmd.AddCommand(deleteCmd)
// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.yamlak.yaml)")

// Cobra also supports local flags, which will only run
// when this action is called directly.
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
44 changes: 22 additions & 22 deletions cmd/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import (

const TEMP_FILE = "/tmp/yamlak.yaml"

var forceCreate bool
var forceCreateFlag bool
var inPlaceFlag bool

// setCmd represents the set command
var setCmd = &cobra.Command{
Expand All @@ -42,14 +43,17 @@ var setCmd = &cobra.Command{
}
defer originalFile.Close()

tmpFile, err := os.OpenFile(TEMP_FILE, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0644)
if err != nil {
return err
var outputFile *os.File = os.Stdout
if inPlaceFlag {
outputFile, err = os.OpenFile(TEMP_FILE, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0644)
if err != nil {
return err
}
defer outputFile.Close()
}
defer tmpFile.Close()

dec := yaml.NewDecoder(originalFile)
enc := yaml.NewEncoder(tmpFile)
enc := yaml.NewEncoder(outputFile)

for {
var doc interface{}
Expand All @@ -60,7 +64,7 @@ var setCmd = &cobra.Command{
}

if CheckConditions(doc, conditions) {
if forceCreate {
if forceCreateFlag {
if err := yamlak.CreateValueByQuery(doc, nodePath, value); err != nil {
return err
}
Expand All @@ -79,12 +83,16 @@ var setCmd = &cobra.Command{
if err := originalFile.Close(); err != nil {
return err
}
if err := tmpFile.Close(); err != nil {
return err
}
// if we want to make changes to a file and not print the output we have to close file and
// move our results to given path
if inPlaceFlag {
if err := outputFile.Close(); err != nil {
return err
}

if err := os.Rename(TEMP_FILE, filePath); err != nil {
return err
if err := os.Rename(TEMP_FILE, filePath); err != nil {
return err
}
}

return nil
Expand All @@ -100,14 +108,6 @@ func init() {
return nil
})
setCmd.Flags().StringSliceVar(&conditions, "condition", []string{}, "condition for target objects to fulfill")
setCmd.Flags().BoolVarP(&forceCreate, "force", "f", false, "use to force creation of node path in file")
// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// setCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// setCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
setCmd.Flags().BoolVarP(&forceCreateFlag, "force", "f", false, "use to force creation of node path in file")
setCmd.Flags().BoolVarP(&inPlaceFlag, "in-place", "i", false, "use to modify given file and not output to stdout")
}

0 comments on commit d36c2af

Please sign in to comment.