Skip to content

Commit

Permalink
feat: Init Command
Browse files Browse the repository at this point in the history
  • Loading branch information
sh0rez committed Jul 22, 2019
1 parent bf544a6 commit ff8857c
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 11 deletions.
81 changes: 81 additions & 0 deletions cmd/tk/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"

"github.com/sh0rez/tanka/pkg/config/v1alpha1"
"github.com/spf13/cobra"
)

// initCmd creates a new application
func initCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "init",
Short: "Create the directory structure",
}
force := cmd.Flags().BoolP("force", "f", false, "ignore the working directory not being empty")
provider := cmd.Flags().StringP("provider", "p", "", fmt.Sprintf("one of %s", listProviders()))
cmd.Run = func(cmd *cobra.Command, args []string) {
files, err := ioutil.ReadDir(".")
if err != nil {
log.Fatalln("Error listing files:", err)
}
if len(files) > 0 && !*force {
log.Fatalln("Error: directory not empty. Use `-f` to force")
}

if err := writeNewFile("jsonnetfile.json", "{}"); err != nil {
log.Fatalln("Error creating `jsonnetfile.json`:", err)
}

if err := os.Mkdir("vendor", os.ModePerm); err != nil {
log.Fatalln("Error creating `vendor/` folder:", err)
}

if err := os.Mkdir("lib", os.ModePerm); err != nil {
log.Fatalln("Error creating `vendor/` folder:", err)
}

if err := os.MkdirAll("environments/default", os.ModePerm); err != nil {
log.Fatalln("Error creating environments folder")
}

if err := writeNewFile("environments/default/main.jsonnet", "{}"); err != nil {
log.Fatalln("Error creating `main.jsonnet`:", err)
}

cfg := v1alpha1.Config{
APIVersion: "tanka.dev/v1alpha1",
Kind: "Environment",
}
if *provider != "" {
cfg.Spec = map[string]interface{}{}
cfg.Spec["provider"] = map[string]interface{}{
*provider: map[string]interface{}{},
}
}

spec, err := json.Marshal(&cfg)
if err != nil {
log.Fatalln("Error creating spec.json:", err)
}

if err := writeNewFile("environments/default/spec.json", string(spec)); err != nil {
log.Fatalln("Error creating `environments/default/spec.json`:", err)
}

}
return cmd
}

// writeNewFile writes the content to a file if it does not exist
func writeNewFile(name, content string) error {
if _, err := os.Stat(name); os.IsNotExist(err) {
return ioutil.WriteFile(name, []byte(content), 0644)
}
return nil
}
10 changes: 7 additions & 3 deletions cmd/tk/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ package main

import (
"log"
"os"

"github.com/mitchellh/mapstructure"
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/sh0rez/tanka/pkg/config/v1alpha1"
"github.com/sh0rez/tanka/pkg/provider"
"github.com/sh0rez/tanka/pkg/provider/kubernetes"

"github.com/mitchellh/mapstructure"
)

var Version = "dev"
Expand Down Expand Up @@ -57,6 +57,7 @@ func main() {
// jsonnet commands
rootCmd.AddCommand(
evalCmd(),
initCmd(),
fmtCmd(),
debugCmd(),
)
Expand All @@ -77,7 +78,10 @@ func main() {
if err := viper.ReadInConfig(); err != nil {
// just run fine without config. Provider features won't work (apply, show, diff)
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
log.Fatalln(rootCmd.Execute())
if err := rootCmd.Execute(); err != nil {
log.Fatalln("Ouch:", err)
}
os.Exit(1)
}

log.Fatalln(err)
Expand Down
20 changes: 12 additions & 8 deletions cmd/tk/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,23 @@ func providerListCmd() *cobra.Command {
Use: "list",
Short: "print all available providers",
Run: func(cmd *cobra.Command, args []string) {
keys := make([]string, len(providers))

i := 0
for k := range providers {
keys[i] = k
i++
}
log.Println("Available providers:", keys)
log.Println("Available providers:", listProviders())
},
}
return cmd
}

func listProviders() []string {
keys := make([]string, len(providers))

i := 0
for k := range providers {
keys[i] = k
i++
}
return keys
}

func applyCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "apply",
Expand Down

0 comments on commit ff8857c

Please sign in to comment.