-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: b3lbctl init instances command #45
- Loading branch information
Showing
9 changed files
with
238 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,23 @@ | ||
package init | ||
|
||
type InitConfigFlags struct { | ||
// ConfigFlags represents `b3lbctl init config` flags | ||
type ConfigFlags struct { | ||
B3LB string | ||
APIKey string | ||
Destination string | ||
} | ||
|
||
func NewInitConfigFlags() *InitConfigFlags { | ||
return &InitConfigFlags{} | ||
// NewInitConfigFlags initialize a new InitConfigFlags struct | ||
func NewInitConfigFlags() *ConfigFlags { | ||
return &ConfigFlags{} | ||
} | ||
|
||
// InstancesFlags represents `b3lbctl init instances` flags | ||
type InstancesFlags struct { | ||
Destination string | ||
} | ||
|
||
// NewInitInstancesFlags initialize a new InitInstancesFlags struct | ||
func NewInitInstancesFlags() *InstancesFlags { | ||
return &InstancesFlags{} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package init | ||
|
||
import ( | ||
"os" | ||
) | ||
|
||
func fileAlreadyExists(path string) bool { | ||
info, _ := os.Stat(path) | ||
return info != nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package init | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path" | ||
|
||
b3lbadmin "github.com/SLedunois/b3lb/v2/pkg/admin" | ||
b3lbconfig "github.com/SLedunois/b3lb/v2/pkg/config" | ||
"github.com/spf13/cobra" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
const instancesConfigFilename = "instances.yml" | ||
|
||
// InstancesCmd represents the `b3lbctl init instances` command | ||
type InstancesCmd struct { | ||
Command *cobra.Command | ||
Flags *InstancesFlags | ||
} | ||
|
||
// NewInitInstancesCmd initialize a new InitInstancesCmd | ||
func NewInitInstancesCmd() *cobra.Command { | ||
cmd := &InstancesCmd{ | ||
Command: &cobra.Command{ | ||
Use: "instances [flags]", | ||
Short: "Initialize b3lb instances file", | ||
Long: "Create instances list file if it does not exists", | ||
}, | ||
Flags: NewInitInstancesFlags(), | ||
} | ||
|
||
cmd.ApplyFlags() | ||
|
||
cmd.Command.RunE = cmd.init | ||
cmd.Command.PreRunE = cmd.preRunE | ||
|
||
return cmd.Command | ||
} | ||
|
||
// ApplyFlags apply command flags to InitInstancesCmd | ||
func (cmd *InstancesCmd) ApplyFlags() { | ||
cmd.Command.Flags().StringVarP(&cmd.Flags.Destination, "dest", "d", b3lbconfig.DefaultConfigFolder, "File folder destination") | ||
} | ||
|
||
func (cmd *InstancesCmd) init(command *cobra.Command, args []string) error { | ||
destFile := path.Join(cmd.Flags.Destination, instancesConfigFilename) | ||
if fileAlreadyExists(destFile) { | ||
return fmt.Errorf("instances configuration file already exists. Please consider editing %s file", destFile) | ||
} | ||
|
||
instances := &b3lbadmin.InstanceList{ | ||
Kind: "InstanceList", | ||
} | ||
|
||
if err := os.MkdirAll(cmd.Flags.Destination, fsRights); err != nil { | ||
return fmt.Errorf("unable to create destination folder: %s", err.Error()) | ||
} | ||
|
||
content, err := yaml.Marshal(instances) | ||
if err != nil { | ||
return fmt.Errorf("unable to marshal yaml instances file: %s", err.Error()) | ||
} | ||
|
||
if err := os.WriteFile(destFile, content, fsRights); err != nil { | ||
return fmt.Errorf("failed to write instances file: %s", err.Error()) | ||
} | ||
|
||
command.Println(fmt.Sprintf("instances file successfully initialized. Please check %s file", destFile)) | ||
|
||
return nil | ||
} | ||
|
||
func (cmd *InstancesCmd) preRunE(command *cobra.Command, args []string) error { | ||
cmd.Command.SilenceUsage = true | ||
dest, err := processDestination(cmd.Flags.Destination) | ||
if err == nil { | ||
cmd.Flags.Destination = dest | ||
return nil | ||
} | ||
|
||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package init | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
b3lbadmin "github.com/SLedunois/b3lb/v2/pkg/admin" | ||
"github.com/SLedunois/b3lbctl/internal/test" | ||
"github.com/stretchr/testify/assert" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
func TestInitInstances(t *testing.T) { | ||
homedir, err := os.UserHomeDir() | ||
if err != nil { | ||
t.Fatal(err) | ||
return | ||
} | ||
|
||
os.Remove(fmt.Sprintf("%s/.b3lb", homedir)) | ||
|
||
tests := []test.CmdTest{ | ||
{ | ||
Name: "a valid command should init instances configuration file", | ||
Mock: func() {}, | ||
Args: []string{}, | ||
Validator: func(t *testing.T, output *bytes.Buffer, err error) { | ||
file := fmt.Sprintf("%s/.b3lb/instances.yml", homedir) | ||
b, err := os.ReadFile(file) | ||
if err != nil { | ||
t.Fatal(err) | ||
return | ||
} | ||
|
||
var instances b3lbadmin.InstanceList | ||
if err := yaml.Unmarshal(b, &instances); err != nil { | ||
t.Fatal(err) | ||
return | ||
} | ||
|
||
assert.Equal(t, "InstanceList", instances.Kind) | ||
assert.Equal(t, 0, len(instances.Instances)) | ||
}, | ||
}, | ||
{ | ||
Name: "an existing file should return an error", | ||
Mock: func() {}, | ||
Args: []string{}, | ||
Validator: func(t *testing.T, output *bytes.Buffer, err error) { | ||
assert.NotNil(t, err) | ||
assert.Equal(t, "instances configuration file already exists. Please consider editing /home/codespace/.b3lb/instances.yml file", err.Error()) | ||
}, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.Name, func(t *testing.T) { | ||
b := bytes.NewBufferString("") | ||
cmd := NewInitInstancesCmd() | ||
cmd.SetArgs(test.Args) | ||
cmd.SetOut(b) | ||
test.Mock() | ||
err := cmd.Execute() | ||
test.Validator(t, b, err) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package init | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path" | ||
|
||
b3lbconfig "github.com/SLedunois/b3lb/v2/pkg/config" | ||
) | ||
|
||
func processDestination(dest string) (string, error) { | ||
if dest == b3lbconfig.DefaultConfigFolder { | ||
formalized, err := formalizeDefaultConfigFolder() | ||
if err != nil { | ||
return "", fmt.Errorf("unable to initialize configuration: %s", err.Error()) | ||
} | ||
|
||
dest = formalized | ||
} | ||
|
||
return dest, nil | ||
} | ||
|
||
func formalizeDefaultConfigFolder() (string, error) { | ||
home, err := os.UserHomeDir() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return path.Join(home, ".b3lb"), nil | ||
} |