Skip to content

Commit

Permalink
Merge pull request #162 from ovotech/validate-config
Browse files Browse the repository at this point in the history
Add validation of app config
  • Loading branch information
Chris Every authored Oct 18, 2019
2 parents 4491e1e + 65d478f commit d4b8145
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 17 deletions.
22 changes: 17 additions & 5 deletions cmd/cobra/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,27 @@ import (
"fmt"
"os"

"github.com/ovotech/cloud-key-rotator/pkg/log"
"github.com/spf13/cobra"
)

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "cloud-key-rotator",
Short: "Rotates cloud keys",
Long: ``,
}
var (
rootCmd = &cobra.Command{
Use: "cloud-key-rotator",
Short: "Rotates cloud keys",
Long: ``,
}
account string
configPath string
defaultConfigPath = "/etc/cloud-key-rotator/"
provider string
project string
defaultAccount string
defaultProvider string
defaultProject string
logger = log.StdoutLogger().Sugar()
)

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
Expand Down
12 changes: 1 addition & 11 deletions cmd/cobra/rotate.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package cmd

import (
"github.com/ovotech/cloud-key-rotator/pkg/config"
"github.com/ovotech/cloud-key-rotator/pkg/log"
"github.com/ovotech/cloud-key-rotator/pkg/rotate"
"github.com/spf13/cobra"
)
Expand All @@ -30,23 +29,14 @@ var (
logger.Info("cloud-key-rotator rotate called")
var err error
var c config.Config
if c, err = config.GetConfig(defaultConfigPath); err == nil {
if c, err = config.GetConfig(configPath); err == nil {
err = rotate.Rotate(account, provider, project, c)
}
if err != nil {
logger.Error(err)
}
},
}
account string
configPath string
defaultConfigPath = "/etc/cloud-key-rotator/"
provider string
project string
defaultAccount string
defaultProvider string
defaultProject string
logger = log.StdoutLogger().Sugar()
)

func init() {
Expand Down
42 changes: 42 additions & 0 deletions cmd/cobra/validate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2019 OVO Technology
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
"github.com/ovotech/cloud-key-rotator/pkg/config"
"github.com/spf13/cobra"
)

var (
validateCmd = &cobra.Command{
Use: "validate",
Short: "Validate cloud-key-rotator config",
Long: `Validate cloud-key-rotator config`,
Run: func(cmd *cobra.Command, args []string) {
logger.Info("validating cloud-key-rotator config")
if _, err := config.GetConfig(configPath); err != nil {
logger.Error(err)
} else {
logger.Infof("cloud-key-rotator config in %s is valid", configPath)
}
},
}
)

func init() {
validateCmd.Flags().StringVarP(&configPath, "config", "c", defaultConfigPath,
"Absolute path of application config")
rootCmd.AddCommand(validateCmd)
}
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ cloud.google.com/go/pubsub v1.0.1 h1:W9tAK3E57P75u0XLLR82LZyw8VpAnhmyTOxW9qzmyj8
cloud.google.com/go/pubsub v1.0.1/go.mod h1:R0Gpsv3s54REJCy4fxDixWD93lHJMoZTyQ2kNxGRt3I=
cloud.google.com/go/storage v1.0.0 h1:VV2nUM3wwLLGh9lSABFgZMjInyUbJeaRSE64WuAIQ+4=
cloud.google.com/go/storage v1.0.0/go.mod h1:IhtSnM/ZTZV8YYJWCY8RULGVqBDmpoyjwiyrjsg+URw=
cloud.google.com/go/storage v1.1.1 h1:ycCxVkVbeNQj8t43giBuzCUJb9g5j1QHua8es8DMb/E=
cloud.google.com/go/storage v1.1.1/go.mod h1:nbQkUX8zrWh07WKekXr/Phd0q/ERj4IOJnkE+v56Qys=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
Expand Down Expand Up @@ -283,6 +284,7 @@ golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4 h1:HuIa8hRrWRSrqYzx1qI49N
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20191002192127-34f69633bfdc h1:c0o/qxkaO2LF5t6fQrT4b5hzyggAkLLlCUjqfRxd8Q4=
golang.org/x/crypto v0.0.0-20191002192127-34f69633bfdc/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 h1:ObdrDkeb4kJdCP557AjRjq69pTHfNouLtWZG7j9rPN8=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
Expand Down Expand Up @@ -367,6 +369,7 @@ golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e h1:D5TXcfTk7xF7hvieo4QErS3qq
golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191008105621-543471e840be h1:QAcqgptGM8IQBC9K/RC4o+O9YmqEm0diQn9QmZw/0mU=
golang.org/x/sys v0.0.0-20191008105621-543471e840be/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191010194322-b09406accb47 h1:/XfQ9z7ib8eEJX2hdgFTZJ/ntt0swNk5oYBziWeTCvY=
golang.org/x/sys v0.0.0-20191010194322-b09406accb47/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down
4 changes: 3 additions & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ func GetConfig(configPath string) (c Config, err error) {
viper.SetEnvPrefix("ckr")
viper.SetConfigName("config")
viper.AddConfigPath(".")
viper.ReadInConfig()
if err = viper.ReadInConfig(); err != nil {
return
}
if err = viper.Unmarshal(&c); err != nil {
return
}
Expand Down

0 comments on commit d4b8145

Please sign in to comment.