-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Configuration system for knoxite #120
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
3fe935c
Create a utils/utils.go file for commonly used methods
penguwin 3325469
Move utility functions for compression from store.go to utils.go
penguwin 9b559b8
Move encryption utility methods from store.go to utils.go
penguwin e15e6d0
Move password input utility methods from repository.go to utils.go
penguwin e5af2b3
Add configuration system backend to knoxite
penguwin 93a9827
Let main.go initialize a configuration
penguwin dfa25f7
Add a config command to knoxite
penguwin 2b9a8da
Add 'set' subcommand to config command
penguwin f323f87
Add config info and config cat commands
penguwin 468c145
Add 'config alias' command
penguwin 58ccc4b
Add configureStoreOpts to store.go to respect values set in config
penguwin 5d2951c
Use configureStoreOpts in clone.go for configuration values
penguwin 4ef7f67
Respect repo paths set in configuration for the repository command
penguwin 1eec766
Fix possible type conversion errors for the failure tolerance
penguwin 2c01c0d
Rename the 'cfg' package to 'config'
penguwin File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
/* | ||
* knoxite | ||
* Copyright (c) 2020, Nicolas Martin <penguwin@penguwin.eu> | ||
* | ||
* For license see LICENSE | ||
*/ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"log" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/knoxite/knoxite/config" | ||
"github.com/muesli/gotable" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var ( | ||
configCmd = &cobra.Command{ | ||
Use: "config", | ||
Short: "manage configuration", | ||
Long: `The config command manages the knoxite configuration`, | ||
} | ||
configInitCmd = &cobra.Command{ | ||
Use: "init", | ||
Short: "initialize a new configuration", | ||
Long: "The init command initializes a new configuration file", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return executeConfigInit() | ||
}, | ||
} | ||
configAliasCmd = &cobra.Command{ | ||
Use: "alias <alias>", | ||
Short: "Set an alias for the storage backend url to a repository", | ||
Long: `The set command adds an alias for the storage backend url to a repository`, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
if len(args) != 1 { | ||
return fmt.Errorf("alias needs an ALIAS to set") | ||
} | ||
return executeConfigAlias(args[0]) | ||
}, | ||
} | ||
configSetCmd = &cobra.Command{ | ||
Use: "set <option> <value>", | ||
Short: "set configuration values for an alias", | ||
Long: "The set command lets you set configuration values for an alias", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
if len(args) < 1 { | ||
return fmt.Errorf("set needs to know which option to set") | ||
} | ||
if len(args) < 2 { | ||
return fmt.Errorf("set needs to know which value to set") | ||
} | ||
return executeConfigSet(args[0], args[1]) | ||
}, | ||
} | ||
configInfoCmd = &cobra.Command{ | ||
Use: "info", | ||
Short: "display information about the configuration file on stdout", | ||
Long: `The info command displays information about the configuration file on stdout`, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return executeConfigInfo() | ||
}, | ||
} | ||
configCatCmd = &cobra.Command{ | ||
Use: "cat", | ||
Short: "display the configuration file on stdout", | ||
Long: `The cat command displays the configuration file on stdout`, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return executeConfigCat() | ||
}, | ||
} | ||
) | ||
|
||
func init() { | ||
configCmd.AddCommand(configInitCmd) | ||
configCmd.AddCommand(configSetCmd) | ||
configCmd.AddCommand(configAliasCmd) | ||
configCmd.AddCommand(configInfoCmd) | ||
configCmd.AddCommand(configCatCmd) | ||
RootCmd.AddCommand(configCmd) | ||
} | ||
|
||
func executeConfigInit() error { | ||
log.Printf("Writing configuration file to: %s\n", cfg.URL().Path) | ||
return cfg.Save() | ||
} | ||
|
||
func executeConfigAlias(alias string) error { | ||
// At first check if the configuration file already exists | ||
cfg.Repositories[alias] = config.RepoConfig{ | ||
Url: globalOpts.Repo, | ||
// Compression: utils.CompressionText(knoxite.CompressionNone), | ||
// Tolerance: 0, | ||
// Encryption: utils.EncryptionText(knoxite.EncryptionAES), | ||
} | ||
|
||
return cfg.Save() | ||
} | ||
|
||
func executeConfigSet(option string, value string) error { | ||
// This probably wont scale for more complex configuration options but works | ||
// fine for now. | ||
parts := strings.Split(option, ".") | ||
if len(parts) != 2 { | ||
return fmt.Errorf("config set needs to work on an alias and a option like this: alias.option") | ||
} | ||
|
||
// The first part should be the repos alias | ||
repo, ok := cfg.Repositories[strings.ToLower(parts[0])] | ||
if !ok { | ||
return fmt.Errorf("No alias with name %s found", parts[0]) | ||
} | ||
|
||
opt := strings.ToLower(parts[1]) | ||
switch opt { | ||
case "url": | ||
repo.Url = value | ||
case "compression": | ||
repo.Compression = value | ||
case "encryption": | ||
repo.Encryption = value | ||
case "tolerance": | ||
tol, err := strconv.Atoi(value) | ||
if err != nil { | ||
return fmt.Errorf("Failed to convert %s to uint for the fault tolerance option: %v", opt, err) | ||
} | ||
repo.Tolerance = uint(tol) | ||
default: | ||
return fmt.Errorf("Unknown configuration option: %s", opt) | ||
} | ||
cfg.Repositories[strings.ToLower(parts[0])] = repo | ||
|
||
return cfg.Save() | ||
} | ||
|
||
func executeConfigInfo() error { | ||
tab := gotable.NewTable( | ||
[]string{"Alias", "Storage URL", "Compression", "Tolerance", "Encryption"}, | ||
[]int64{-15, -35, -15, -15, 15}, | ||
"No repository configurations found.") | ||
|
||
for alias, repo := range cfg.Repositories { | ||
tab.AppendRow([]interface{}{ | ||
alias, | ||
repo.Url, | ||
repo.Compression, | ||
fmt.Sprintf("%v", repo.Tolerance), | ||
repo.Encryption, | ||
}) | ||
} | ||
return tab.Print() | ||
} | ||
|
||
func executeConfigCat() error { | ||
json, err := json.MarshalIndent(cfg, "", " ") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Printf("%s\n", json) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This may be the wrong position to critizise this here, since we're doing this in every command - but shouldn't we just print the help text of this command reather than a specific help text? I often had the situation where i typed just the second argument of a command requiring two arguments and was confused when reading that the second argument was missing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, let's keep this discussion in a separate issue / ticket.