Skip to content

Commit

Permalink
Merge pull request #158 from DopplerHQ/ruud_add_doppler_config_support
Browse files Browse the repository at this point in the history
Add repo config file functionality DPLR-712
  • Loading branch information
rgmvisser authored Nov 12, 2020
2 parents 73e0e3c + 01b928c commit 3fbb3a0
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 0 deletions.
3 changes: 3 additions & 0 deletions doppler.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
setup:
project: cli
config: dev
17 changes: 17 additions & 0 deletions pkg/cmd/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"strings"

"github.com/DopplerHQ/cli/pkg/configuration"
"github.com/DopplerHQ/cli/pkg/controllers"
"github.com/DopplerHQ/cli/pkg/http"
"github.com/DopplerHQ/cli/pkg/models"
"github.com/DopplerHQ/cli/pkg/printer"
Expand Down Expand Up @@ -56,6 +57,12 @@ func setup(cmd *cobra.Command, args []string) {
}
}

repoConfig, err := controllers.RepoConfig()
if !err.IsNil() {
utils.Log(err.Message)
utils.LogDebugError(err.Unwrap())
}

currentProject := localConfig.EnclaveProject.Value
selectedProject := ""

Expand All @@ -66,6 +73,11 @@ func setup(cmd *cobra.Command, args []string) {
utils.Log(valueFromEnvironmentNotice("DOPPLER_PROJECT"))
selectedProject = localConfig.EnclaveProject.Value
default:
if repoConfig.Setup.Project != "" {
utils.Log("Reading project from repo config file doppler.yaml")
selectedProject = repoConfig.Setup.Project
break
}
projects, httpErr := http.GetProjects(localConfig.APIHost.Value, utils.GetBool(localConfig.VerifyTLS.Value, true), localConfig.Token.Value)
if !httpErr.IsNil() {
utils.HandleError(httpErr.Unwrap(), httpErr.Message)
Expand All @@ -90,6 +102,11 @@ func setup(cmd *cobra.Command, args []string) {
utils.Log(valueFromEnvironmentNotice("DOPPLER_CONFIG"))
selectedConfig = localConfig.EnclaveConfig.Value
default:
if repoConfig.Setup.Config != "" {
utils.Log("Reading config from repo config file doppler.yaml")
selectedConfig = repoConfig.Setup.Config
break
}
configs, apiError := http.GetConfigs(localConfig.APIHost.Value, utils.GetBool(localConfig.VerifyTLS.Value, true), localConfig.Token.Value, selectedProject)
if !apiError.IsNil() {
utils.HandleError(apiError.Unwrap(), apiError.Message)
Expand Down
60 changes: 60 additions & 0 deletions pkg/controllers/repo_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
Copyright © 2020 Doppler <[email protected]>
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 controllers

import (
"fmt"
"io/ioutil"
"path/filepath"

"github.com/DopplerHQ/cli/pkg/models"
"github.com/DopplerHQ/cli/pkg/utils"
"gopkg.in/yaml.v3"
)

// repoConfigFileName (doppler.yaml)
const repoConfigFileName = "doppler.yaml"

// RepoConfig Reads the configuration file (doppler.yaml) if exists and returns the set configuration
func RepoConfig() (models.RepoConfig, Error) {

RepoConfigFile := filepath.Join("./", repoConfigFileName)

if utils.Exists(RepoConfigFile) {
utils.LogDebug(fmt.Sprintf("Reading repo config file %s", RepoConfigFile))

yamlFile, err := ioutil.ReadFile(RepoConfigFile) // #nosec G304

if err != nil {
var e Error
e.Err = err
e.Message = "Unable to read doppler repo config file"
return models.RepoConfig{}, e
}

var repoConfig models.RepoConfig

if err := yaml.Unmarshal(yamlFile, &repoConfig); err != nil {
var e Error
e.Err = err
e.Message = "Unable to parse doppler repo config file"
return models.RepoConfig{}, e
}

return repoConfig, Error{}
}
return models.RepoConfig{}, Error{}
}
25 changes: 25 additions & 0 deletions pkg/models/repo_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
Copyright © 2020 Doppler <[email protected]>
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 models

// RepoConfig holds all repo configuration
type RepoConfig struct {
Setup struct {
Config string `yaml:"config"`
Project string `yaml:"project"`
} `yaml:"setup"`
}

0 comments on commit 3fbb3a0

Please sign in to comment.