Skip to content

Commit

Permalink
feat: add init sub command to create Giltfile
Browse files Browse the repository at this point in the history
  • Loading branch information
retr0h committed Jan 1, 2024
1 parent f78cc00 commit 88fc303
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ cobertura.xml
cover.out
dist/
result.xml
test/integration/tmp/
.task/
test/integration/tmp/
91 changes: 91 additions & 0 deletions cmd/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Copyright (c) 2023 John Dewey

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

package cmd

import (
"bytes"
"fmt"
"log/slog"
"os"

"github.com/spf13/cobra"
"github.com/spf13/viper"
"gopkg.in/yaml.v3"

"github.com/retr0h/go-gilt/pkg/config"
)

// initCmd represents the init command
var initCmd = &cobra.Command{
Use: "init",
Short: "Initialize Gilt with a Giltfile",
Long: `Initializes Gilt by creating a default config file in the shell's
current working directory.`,
Run: func(cmd *cobra.Command, args []string) {
var b bytes.Buffer

// set configFile defaults
repo := []config.Repository{
{
Git: "",
Version: "",
DstDir: "",
},
}
viper.SetDefault("repositories", repo)
c := viper.AllSettings()

ye := yaml.NewEncoder(&b)
ye.SetIndent(2)
if err := ye.Encode(c); err != nil {
logger.Error(
"failed to encode file",
slog.String("err", err.Error()),
)
os.Exit(1)
}

configFile := viper.GetString("giltFile")
_, err := os.Stat(configFile)
if err == nil {
logger.Error(
"file already exists",
slog.String("Giltfile", configFile),
)
os.Exit(1)
}

if err := os.WriteFile(configFile, b.Bytes(), 0o666); err != nil {
logger.Error(
"failed to write file",
slog.String("Giltfile", viper.ConfigFileUsed()),
slog.String("err", err.Error()),
)
os.Exit(1)
}

fmt.Printf("wrote %s\n", configFile)
},
}

func init() {
rootCmd.AddCommand(initCmd)
}
6 changes: 4 additions & 2 deletions cmd/overlay.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ import (

// overlayCmd represents the overlay command
var overlayCmd = &cobra.Command{
Use: "overlay",
Short: "Install gilt dependencies",
Use: "overlay",
Short: "Install Gilt dependencies",
Long: `Overlay the repositories from the Giltfile into their respective
destinations.`,
PersistentPreRun: initConfig,
RunE: func(cmd *cobra.Command, args []string) error {
// By the time we reach this point, we know that the arguments were
Expand Down
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ func init() {
_ = viper.BindPFlag("giltFile", rootCmd.PersistentFlags().Lookup("gilt-file"))
_ = viper.BindPFlag("giltDir", rootCmd.PersistentFlags().Lookup("gilt-dir"))
_ = viper.BindPFlag("repositories", rootCmd.PersistentFlags().Lookup("repositories"))

initLogger()
}

func initLogger() {
Expand Down
8 changes: 8 additions & 0 deletions docs/docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ sidebar_position: 4

## CLI

### Init Configuration

Initializes config file in the shell's current working directory:

```bash
gilt init
```

### Overlay Repository

Overlay a remote repository into the destination provided.
Expand Down
8 changes: 8 additions & 0 deletions test/integration/test_cli.bats
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ teardown() {
rm -rf ${GILT_ROLES_DIR}
rm -rf ${GILT_TEST_DIR}
rm -f ${GILT_TEST_BASE_TMP_DIR}/Giltfile.yaml
rm -f /tmp/initGiltfile.yaml
}

@test "invoke gilt without arguments prints usage" {
Expand Down Expand Up @@ -198,3 +199,10 @@ teardown() {
run stat ${GILT_TEST_BASE_TMP_DIR}/retr0h.ansible-etcd/
[ "$status" = 0 ]
}

@test "invoke gilt init" {
run bash -c "cd ${GILT_TEST_BASE_TMP_DIR}; go run ${GILT_PROGRAM} init -f /tmp/initGiltfile.yaml"

run stat /tmp/initGiltfile.yaml
[ "$status" = 0 ]
}

0 comments on commit 88fc303

Please sign in to comment.