Commit df2ea1a4 authored by Christopher Harm's avatar Christopher Harm

Adding config commands

parent 0d0bf537
......@@ -15,19 +15,48 @@
package cmd
import (
"errors"
"os"
"git.psu.edu/k8s/devtool/config"
"git.psu.edu/k8s/devtool/environment"
"github.com/fatih/color"
"github.com/google/uuid"
"github.com/spf13/cobra"
)
var skipCompile bool
var runTests bool
var openServices bool
// buildCmd represents the build command
var buildCmd = &cobra.Command{
Use: "build",
Short: "build the application",
Run: func(cmd *cobra.Command, args []string) {
build()
err := environment.Run(true, "minikube", "status")
if err != nil {
color.Red("Minikube is not started... run `devtool start` first.")
return
}
conf, err := config.New(configFile)
if err != nil {
color.New(color.FgRed).PrintFunc()("Error reading configuration file: %s", configFile)
return
}
err = build(conf)
if err != nil {
color.Red("Failed to build application.")
return
}
err = buildDockerAndDeploy(conf)
if err != nil {
color.Red("Failed to build docker image.")
return
}
},
}
......@@ -45,18 +74,83 @@ func init() {
// buildCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
buildCmd.Flags().BoolVar(&skipCompile, "skip-compile", false, "Skip the compile step")
buildCmd.Flags().BoolVar(&runTests, "tests", false, "Run unit tests during the build phase")
buildCmd.Flags().BoolVar(&openServices, "open", false, "Open browser to the services after deploying")
}
func build() {
err := environment.Run(true, "minikube", "status")
if err != nil {
color.Red("Minikube is not started... run `devtool start` first.")
return
}
func build(config config.Config) error {
if skipCompile {
color.Yellow("Skipping Build Step.")
} else {
color.Blue("Building Application")
return nil
}
color.Blue("Building Application")
switch lang := config.Language; lang {
case "java":
args := []string{"clean", "package", "-Ddependency-check.skip=true"}
if !runTests {
args = append(args, "-DskipTests")
}
environment.RunRequired(true, "mvn", args...)
case "golang":
fallthrough
case "go":
environment.RunRequired(true, "make", "build")
default:
color.New(color.FgRed).PrintFunc()("devtool doesn't support the '%s' language yet.", lang)
return errors.New("Language not supported")
}
return nil
}
func buildDockerAndDeploy(config config.Config) error {
color.Blue("Building Docker Containers")
for _, deployable := range config.Deployables {
dockerfile := deployable.Dockerfile
image := deployable.Name
tag, err := uuid.NewRandom()
if err != nil {
color.Red("Failed to generate UUID for docker tag")
return err
}
dockerTag := image + ":" + tag.String()
color.New(color.FgGreen).PrintFunc()("Building %s", dockerTag)
err = environment.Run(true, "docker", "build", "-t", dockerTag, dockerfile)
if err != nil {
return err
}
color.Blue("Installing Chart")
releaseName := deployable.Name + "-local"
args := []string{"upgrade", releaseName, "--install", deployable.Chart, "--version", deployable.ChartVersion}
if len(config.LocalEnvVars) > 0 {
for _, key := range config.LocalEnvVars {
args = append(args, "--set", "environmentVariables."+key+"="+os.Getenv(key))
}
}
if len(config.LocalSecrets) > 0 {
for _, key := range config.LocalSecrets {
args = append(args, "--set", "secrets."+key+"="+os.Getenv(key))
}
}
args = append(args, "--set", "image.repository="+image)
args = append(args, "--set", "image.tag="+tag.String())
args = append(args, "-f", deployable.LocalConfig)
err = environment.Run(true, "helm", args...)
if err != nil {
color.New(color.FgRed).PrintFunc()("helm error. try running 'helm del --purge %s'", releaseName)
return err
}
if openServices {
environment.Run(true, "minikube", "service", releaseName)
}
}
return nil
}
// Copyright © 2019 NAME HERE <EMAIL ADDRESS>
//
// 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/spf13/cobra"
)
// configCmd represents the config command
var configCmd = &cobra.Command{
Use: "config",
Short: "Manage the projects build configuration",
// Run: func(cmd *cobra.Command, args []string) {
// fmt.Println("config called")
// },
}
func init() {
rootCmd.AddCommand(configCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// configCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// configCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
// Copyright © 2019 NAME HERE <EMAIL ADDRESS>
//
// 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 (
"fmt"
"github.com/spf13/cobra"
)
// addCmd represents the add command
var addCmd = &cobra.Command{
Use: "add",
Short: "adds a deployable unit to the project configuration",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("add called")
},
}
func init() {
configCmd.AddCommand(addCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// addCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// addCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
// Copyright © 2019 NAME HERE <EMAIL ADDRESS>
//
// 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 (
"fmt"
"github.com/spf13/cobra"
)
// deleteCmd represents the delete command
var deleteCmd = &cobra.Command{
Use: "delete",
Short: "removes a deployable unit to the project configuration",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("delete called")
},
}
func init() {
configCmd.AddCommand(deleteCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// deleteCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// deleteCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
// Copyright © 2019 NAME HERE <EMAIL ADDRESS>
//
// 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 (
"os"
"strings"
"git.psu.edu/k8s/devtool/config"
"github.com/fatih/color"
"github.com/spf13/cobra"
"github.com/tcnksm/go-input"
)
// initCmd represents the init command
var initCmd = &cobra.Command{
Use: "init",
Short: "initialize the project configuration",
Run: func(cmd *cobra.Command, args []string) {
ui := &input.UI{
Writer: os.Stdout,
Reader: os.Stdin,
}
_, err := config.New(configFile)
if err == nil {
color.Red("Configuration already exists.")
response, err := ui.Ask("Are you sure you want to overwrite it?", &input.Options{
Default: "N",
})
if err != nil {
return
}
switch strings.ToLower(response) {
case "":
case "y":
case "yes":
default:
return
}
}
color.Yellow("Writing configuration")
var conf config.Config
conf.Write(configFile)
},
}
func init() {
configCmd.AddCommand(initCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// initCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// initCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
// Copyright © 2019 NAME HERE <EMAIL ADDRESS>
//
// 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 (
"git.psu.edu/k8s/devtool/config"
"github.com/fatih/color"
"github.com/spf13/cobra"
)
// listCmd represents the list command
var listCmd = &cobra.Command{
Use: "list",
Short: "lists the project configuration",
Run: func(cmd *cobra.Command, args []string) {
conf, err := config.New(configFile)
if err != nil {
color.New(color.FgRed).PrintFunc()("Error reading configuration file: %s", configFile)
return
}
print := color.New(color.FgBlue).PrintFunc()
color.White("Project Configuration")
print("Name: %s", conf.Name)
print("Language: %s", conf.Language)
},
}
func init() {
configCmd.AddCommand(listCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// listCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// listCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
......@@ -18,11 +18,16 @@ import (
"fmt"
"os"
"github.com/fatih/color"
homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
const (
configFile string = ".devtool.config"
)
var cfgFile string
// rootCmd represents the base command when called without any subcommands
......@@ -82,3 +87,12 @@ func initConfig() {
fmt.Println("Using config file:", viper.ConfigFileUsed())
}
}
func PrintLogo() {
color.Blue("__________ _____________ ___ .___ __ .__ ")
color.Blue("\\______ \\/ _____/ | \\ __| _/_______ ___/ |_ ____ ____ | | ")
color.Blue(" | ___/\\_____ \\| | / / __ |/ __ \\ \\/ /\\ __\\/ _ \\ / _ \\| | ")
color.Blue(" | | / \\ | / / /_/ \\ ___/\\ / | | ( <_> | <_> ) |__")
color.Blue(" |____| /_______ /______/ \\____ |\\___ >\\_/ |__| \\____/ \\____/|____/")
color.Blue(" \\/ \\/ \\/ ")
}
......@@ -23,7 +23,7 @@ type (
)
func (config Config) Write(filename string) error {
data, err := json.Marshal(config)
data, err := json.MarshalIndent(config, "", " ")
if err != nil {
return err
}
......
......@@ -3,5 +3,6 @@ package main
import "git.psu.edu/k8s/devtool/cmd"
func main() {
cmd.PrintLogo()
cmd.Execute()
}
......@@ -5,10 +5,12 @@ go 1.12
require (
github.com/BurntSushi/toml v0.3.1 // indirect
github.com/fatih/color v1.7.0
github.com/google/uuid v1.1.1
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/mattn/go-colorable v0.1.1 // indirect
github.com/mattn/go-isatty v0.0.7 // indirect
github.com/mitchellh/go-homedir v1.1.0
github.com/spf13/cobra v0.0.3
github.com/spf13/viper v1.3.2
github.com/tcnksm/go-input v0.0.0-20180404061846-548a7d7a8ee8
)
......@@ -10,6 +10,8 @@ github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
......@@ -43,8 +45,11 @@ github.com/spf13/viper v1.3.2 h1:VUFqw5KcqRf7i70GOzW7N+Q7+gxVBkSSqiXB12+JQ4M=
github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/tcnksm/go-input v0.0.0-20180404061846-548a7d7a8ee8 h1:RB0v+/pc8oMzPsN97aZYEwNuJ6ouRJ2uhjxemJ9zvrY=
github.com/tcnksm/go-input v0.0.0-20180404061846-548a7d7a8ee8/go.mod h1:IlWNj9v/13q7xFbaK4mbyzMNwrZLaWSHx/aibKIZuIg=
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9 h1:mKdxBk7AujPs8kU4m80U72y/zjbZ3UcXC7dClwKbUI0=
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223 h1:DH4skfRX4EBpamg7iV4ZlCpblAHI6s6TDM39bFZumv8=
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment