Skip to content

Commit

Permalink
config disable/enable
Browse files Browse the repository at this point in the history
  • Loading branch information
dcoraboeuf committed Mar 28, 2021
1 parent 29154e3 commit d1b28c7
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 6 deletions.
53 changes: 53 additions & 0 deletions cmd/configDisable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
Copyright © 2021 Damien Coraboeuf <damien.coraboeuf@nemerosa.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package cmd

import (
"github.com/spf13/cobra"

config "ontrack-cli/config"
)

// configDisableCmd represents the configDisable command
var configDisableCmd = &cobra.Command{
Use: "disable NAME",
Short: "Disables the NAME configuration",
Long: `Disables the NAME configuration.`,
Args: cobra.ExactValidArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return config.SetConfigurationState(args[0], true)
},
}

func init() {
configCmd.AddCommand(configDisableCmd)

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

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

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// configDisableCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
53 changes: 53 additions & 0 deletions cmd/configEnable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
Copyright © 2021 Damien Coraboeuf <damien.coraboeuf@nemerosa.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package cmd

import (
config "ontrack-cli/config"

"github.com/spf13/cobra"
)

// configEnableCmd represents the configEnable command
var configEnableCmd = &cobra.Command{
Use: "enable NAME",
Short: "Enables the NAME configuration",
Long: `Enables the NAME configuration.`,
Args: cobra.ExactValidArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return config.SetConfigurationState(args[0], false)
},
}

func init() {
configCmd.AddCommand(configEnableCmd)

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

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

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// configEnableCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
10 changes: 8 additions & 2 deletions cmd/configList.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,17 @@ var configListCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
root := config.ReadRootConfiguration()
for _, item := range root.Configurations {
var line string
if item.Name == root.Selected {
fmt.Printf("* %s\n", item.Name)
line += "* "
} else {
fmt.Printf(" %s\n", item.Name)
line += " "
}
line += item.Name
if item.Disabled {
line += " (disabled)"
}
fmt.Println(line)
fmt.Printf(" %s\n", item.URL)
}
},
Expand Down
40 changes: 36 additions & 4 deletions config/configService.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ type Config struct {
Password string
// Token for the remote server (when using token-based authentication)
Token string
// Is this configuration disabled?
Disabled bool
}

// Gets the current configuration
Expand All @@ -53,22 +55,22 @@ func GetSelectedConfiguration() (*Config, error) {
}

// Reads the configuration
func ReadRootConfiguration() RootConfig {
func ReadRootConfiguration() *RootConfig {
var root RootConfig
home, _ := homedir.Dir()
configFilePath := filepath.Join(home, configFileName)

// If the config file does not exist, returns an empty root config
if _, err := os.Stat(configFilePath); err != nil {
if os.IsNotExist(err) {
return root
return &root
}
}

reader, _ := os.Open(configFilePath)
buf, _ := ioutil.ReadAll(reader)
yaml.Unmarshal(buf, &root)
return root
return &root
}

// Adds a new configuration and set as default
Expand Down Expand Up @@ -98,7 +100,7 @@ func AddConfiguration(config Config) error {
}

// Finds an existing configuration
func findConfigurationByName(root RootConfig, name string) *Config {
func findConfigurationByName(root *RootConfig, name string) *Config {
for _, item := range root.Configurations {
if item.Name == name {
return &item
Expand All @@ -107,6 +109,15 @@ func findConfigurationByName(root RootConfig, name string) *Config {
return nil
}

// Replacing an existing configuration
func replaceConfigurationByName(root *RootConfig, config *Config) {
for index, item := range root.Configurations {
if item.Name == config.Name {
root.Configurations[index] = *config
}
}
}

// Sets the new selected configuration
func SetSelectedConfiguration(name string) error {
root := ReadRootConfiguration()
Expand All @@ -128,3 +139,24 @@ func SetSelectedConfiguration(name string) error {
// OK
return nil
}

// Disables or enabled a configuration
func SetConfigurationState(name string, disabled bool) error {
root := ReadRootConfiguration()
existing := findConfigurationByName(root, name)
if existing == nil {
return fmt.Errorf("Configuration with name %s does not exist", name)
}
// Adjust the existing configuration
existing.Disabled = disabled
replaceConfigurationByName(root, existing)
// Saves the root configuration back
home, _ := homedir.Dir()
configFilePath := filepath.Join(home, configFileName)
buf, _ := yaml.Marshal(root)
_, _ = os.OpenFile(configFilePath, os.O_CREATE|os.O_WRONLY, 0600)
_ = ioutil.WriteFile(configFilePath, buf, 0600)

// OK
return nil
}

0 comments on commit d1b28c7

Please sign in to comment.