Skip to content

Commit

Permalink
Merge pull request #28 from asteris-llc/feature/param-acceptance
Browse files Browse the repository at this point in the history
load: accept initial parameters
  • Loading branch information
BrianHicks committed Jun 2, 2016
2 parents 7c947fc + 03495d2 commit 2283556
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 21 deletions.
4 changes: 2 additions & 2 deletions cmd/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"github.com/spf13/cobra"
)

// graphCmd represents the check command
// graphCmd represents the graph command
var graphCmd = &cobra.Command{
Use: "graph",
Short: "graph the execution of a module",
Expand All @@ -37,7 +37,7 @@ var graphCmd = &cobra.Command{
for _, fname := range args {
logger := logrus.WithField("filename", fname)

graph, err := load.Load(fname)
graph, err := load.Load(fname, nil)
if err != nil {
logger.WithError(err).Fatal("could not parse file")
}
Expand Down
34 changes: 34 additions & 0 deletions cmd/params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright © 2016 Asteris, LLC
//
// 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 (
"encoding/json"

"github.com/asteris-llc/converge/resource"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)

func addParamsArguments(flags *pflag.FlagSet) {
flags.StringP("paramsJSON", "p", "{}", "parameters for the top-level module, in JSON format")
}

func getParamsFromFlags() (resource.Values, error) {
params := resource.Values{}
err := json.Unmarshal([]byte(viper.GetString("paramsJson")), &params)

return params, err
}
11 changes: 10 additions & 1 deletion cmd/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/asteris-llc/converge/exec"
"github.com/asteris-llc/converge/load"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// planCmd represents the plan command
Expand All @@ -35,10 +36,15 @@ var planCmd = &cobra.Command{
return nil
},
Run: func(cmd *cobra.Command, args []string) {
params, err := getParamsFromFlags()
if err != nil {
logrus.WithError(err).Fatal("could not load params")
}

for _, fname := range args {
logger := logrus.WithField("filename", fname)

graph, err := load.Load(fname)
graph, err := load.Load(fname, params)
if err != nil {
logger.WithError(err).Fatal("could not parse file")
}
Expand All @@ -56,5 +62,8 @@ var planCmd = &cobra.Command{
}

func init() {
addParamsArguments(planCmd.Flags())
viper.BindPFlags(planCmd.Flags())

RootCmd.AddCommand(planCmd)
}
22 changes: 10 additions & 12 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"fmt"
"os"

"github.com/Sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand All @@ -34,9 +35,9 @@ examples and usage of using your application. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
}

// Execute adds all child commands to the root command sets flags appropriately.
Expand All @@ -51,14 +52,11 @@ func Execute() {
func init() {
cobra.OnInitialize(initConfig)

// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags, which, if defined here,
// will be global for your application.

RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.converge.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")

if err := viper.BindPFlags(RootCmd.PersistentFlags()); err != nil {
logrus.WithError(err).Fatal("could not bind flags")
}
}

// initConfig reads in config file and ENV variables if set.
Expand All @@ -68,8 +66,8 @@ func initConfig() {
}

viper.SetConfigName(".converge") // name of config file (without extension)
viper.AddConfigPath("$HOME") // adding home directory as first search path
viper.AutomaticEnv() // read in environment variables that match
viper.AddConfigPath("$HOME") // adding home directory as first search path
viper.AutomaticEnv() // read in environment variables that match

// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
Expand Down
3 changes: 2 additions & 1 deletion cmd/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

"github.com/Sirupsen/logrus"
"github.com/asteris-llc/converge/load"
"github.com/asteris-llc/converge/resource"
"github.com/spf13/cobra"
)

Expand All @@ -36,7 +37,7 @@ var validateCmd = &cobra.Command{
for _, fname := range args {
logger := logrus.WithField("filename", fname)

_, err := load.Load(fname)
_, err := load.Load(fname, resource.Values{})
if err != nil {
logger.WithError(err).Fatal("could not parse file")
}
Expand Down
3 changes: 2 additions & 1 deletion exec/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ import (

"github.com/asteris-llc/converge/exec"
"github.com/asteris-llc/converge/load"
"github.com/asteris-llc/converge/resource"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestPlan(t *testing.T) {
t.Parallel()

graph, err := load.Load("../samples/basic.hcl")
graph, err := load.Load("../samples/basic.hcl", resource.Values{})
require.NoError(t, err)

results, err := exec.Plan(graph)
Expand Down
3 changes: 2 additions & 1 deletion load/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ import (

// Load a module from a resource. This uses the protocol in the path (or file://
// if not present) to determine from where the module should be loaded.
func Load(source string) (*Graph, error) {
func Load(source string, args resource.Values) (*Graph, error) {
initial, err := loadAny(nil, source)
if err != nil {
return nil, err
}
initial.Args = args

root, err := parseSource(source)
if err != nil {
Expand Down
7 changes: 4 additions & 3 deletions load/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"testing"

"github.com/asteris-llc/converge/load"
"github.com/asteris-llc/converge/resource"
"github.com/stretchr/testify/assert"
)

Expand All @@ -32,19 +33,19 @@ func init() {
}

func TestLoadBasic(t *testing.T) {
_, err := load.Load(path.Join(samplesDir, "basic.hcl"))
_, err := load.Load(path.Join(samplesDir, "basic.hcl"), resource.Values{})
assert.NoError(t, err)
}

func TestLoadNotExist(t *testing.T) {
badPath := path.Join(samplesDir, "doesNotExist.hcl")
_, err := load.Load(badPath)
_, err := load.Load(badPath, resource.Values{})
if assert.Error(t, err) {
assert.EqualError(t, err, fmt.Sprintf("Not found: %q using protocol \"file\"", badPath))
}
}

func TestLoadFileModule(t *testing.T) {
_, err := load.Load(path.Join(samplesDir, "sourceFile.hcl"))
_, err := load.Load(path.Join(samplesDir, "sourceFile.hcl"), resource.Values{})
assert.NoError(t, err)
}

0 comments on commit 2283556

Please sign in to comment.