Skip to content
This repository was archived by the owner on Sep 18, 2021. It is now read-only.

Commit 7304582

Browse files
committed
style(cli): resolve linter issues
1 parent 2378d2f commit 7304582

File tree

11 files changed

+50
-17
lines changed

11 files changed

+50
-17
lines changed

ansible/collection.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/spf13/viper"
1414
)
1515

16+
// GetAnsibleVersion returns the installed Ansible version. Returns error if not installed
1617
func GetAnsibleVersion() (string, error) {
1718
cmd := exec.Command("ansible", "--version")
1819
var stdoutBuffer = new(bytes.Buffer)
@@ -34,6 +35,7 @@ func extractAnsibleVersion(versionCmdOutput string) string {
3435
return r.FindStringSubmatch(versionLine)[1]
3536
}
3637

38+
// GetCollectionDirs returns a list of Ansible collection paths from config or environment
3739
func GetCollectionDirs() ([]string, error) {
3840
var customCollectionPath = viper.GetString("ansible.collection_path")
3941
if customCollectionPath != "" {
@@ -63,6 +65,7 @@ func GetCollectionDirs() ([]string, error) {
6365
return pathList, nil
6466
}
6567

68+
// GetStackHeadCollectionLocation returns the exact path of where the StackHead collection has been installed to
6669
func GetStackHeadCollectionLocation() (string, error) {
6770
collectionDirs, err := GetCollectionDirs()
6871
if err != nil {

ansible/inventory.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,23 @@ import (
77
"text/template"
88
)
99

10+
// InventoryOptions is a configuration used during creation of the temporary inventory file
1011
type InventoryOptions struct {
11-
IpAddress string
12+
IPAddress string
1213
}
1314

14-
func IpAddress(text string) InventoryOption {
15+
// IPAddress sets the corresponding inventory option
16+
func IPAddress(ipAddress string) InventoryOption {
1517
return func(args *InventoryOptions) {
16-
args.IpAddress = text
18+
args.IPAddress = ipAddress
1719
}
1820
}
1921

22+
// InventoryOption is a single inventory setting
2023
type InventoryOption func(*InventoryOptions)
2124

25+
// CreateInventoryFile creates a temporary inventory file with the given settings and returns an absolute file path.
26+
// Note: make sure to remove the file when you don't need it anymore!
2227
func CreateInventoryFile(options ...InventoryOption) (string, error) {
2328
opts := &InventoryOptions{}
2429
for _, setter := range options {
@@ -44,7 +49,7 @@ all:
4449
ansible_connection: ssh
4550
hosts:
4651
mackerel:
47-
ansible_host: {{ .IpAddress }}
52+
ansible_host: {{ .IPAddress }}
4853
`)
4954

5055
if err = t.Execute(tmpFile, opts); err != nil {

commands/init.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ import (
77
"github.com/getstackhead/stackhead/cli/routines"
88
)
99

10+
// Init is a command object for Cobra that provides the init command
1011
var Init = &cobra.Command{
1112
Use: "init",
1213
Short: "Install StackHead dependencies according to configuration file",
1314
Long: `init will install all required dependencies according to configuration file.
1415
If no configuration file exists, it will start a wizard to create one.`,
1516
Run: func(cmd *cobra.Command, args []string) {
1617

17-
routines.RunTask(commands_init.InstallCollection...)
18-
routines.RunTask(commands_init.InstallModules...)
18+
routines.RunTask(commandsinit.InstallCollection...)
19+
routines.RunTask(commandsinit.InstallModules...)
1920
},
2021
}

commands/init/install_collection.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package commands_init
1+
package commandsinit
22

33
import (
44
"fmt"
@@ -25,6 +25,7 @@ func installCollectionDependencies() error {
2525
)
2626
}
2727

28+
// InstallCollection is a list of task options that provide the actual workflow being run
2829
var InstallCollection = []routines.TaskOption{
2930
routines.Text("Installing StackHead Ansible collection"),
3031
routines.Execute(func(wg *sync.WaitGroup, result chan routines.TaskResult) {
@@ -34,7 +35,7 @@ var InstallCollection = []routines.TaskOption{
3435
// Check if Ansible is installed
3536
_, err = ansible.GetAnsibleVersion()
3637
if err != nil {
37-
err = fmt.Errorf("Ansible could not be found on your system. Please install it.")
38+
err = fmt.Errorf(fmt.Sprintf("Ansible could not be found on your system. Please install it."))
3839
}
3940

4041
if err == nil {

commands/init/install_modules.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package commands_init
1+
package commandsinit
22

33
import (
44
"fmt"
@@ -70,6 +70,7 @@ func installStackHeadModules() error {
7070
return nil
7171
}
7272

73+
// InstallModules is a list of task options that provide the actual workflow being run
7374
var InstallModules = []routines.TaskOption{
7475
routines.Text("Installing StackHead modules"),
7576
routines.Execute(func(wg *sync.WaitGroup, result chan routines.TaskResult) {

commands/setup.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/getstackhead/stackhead/cli/routines"
1212
)
1313

14+
// SetupServer is a command object for Cobra that provides the setup command
1415
var SetupServer = &cobra.Command{
1516
Use: "setup [ipv4 address]",
1617
Example: "setup 192.168.178.14",
@@ -24,7 +25,7 @@ var SetupServer = &cobra.Command{
2425
defer wg.Done()
2526

2627
// Generate Inventory file
27-
inventoryFile, err := ansible.CreateInventoryFile(ansible.IpAddress(args[0]))
28+
inventoryFile, err := ansible.CreateInventoryFile(ansible.IPAddress(args[0]))
2829
if err == nil {
2930
defer os.Remove(inventoryFile)
3031
err = routines.ExecAnsiblePlaybook("server-provision", inventoryFile)

commands/validate.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/getstackhead/stackhead/cli/jsonschema"
1010
)
1111

12+
// Validate is a command object for Cobra that provides the validate command
1213
var Validate = &cobra.Command{
1314
Use: "validate [path to project definition file]",
1415
Example: "validate ./my-project-definition.yml",

jsonschema/validation.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ func ValidateFile(filePath string) (*gojsonschema.Result, error) {
4141
}
4242

4343
// YAML to JSON
44-
configJson, err := yaml.YAMLToJSON(configData)
44+
configJSON, err := yaml.YAMLToJSON(configData)
4545
if err != nil {
4646
return nil, err
4747
}
48-
documentLoader := gojsonschema.NewBytesLoader(configJson)
48+
documentLoader := gojsonschema.NewBytesLoader(configJSON)
4949

5050
return gojsonschema.Validate(schemaLoader, documentLoader)
5151
}

routines/exec.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/getstackhead/stackhead/cli/ansible"
1313
)
1414

15+
// Exec is a wrapper function around exec.Command with additional settings for this CLI
1516
func Exec(name string, arg ...string) error {
1617
cmd := exec.Command(name, arg...)
1718
var errBuffer = new(bytes.Buffer)
@@ -28,6 +29,7 @@ func Exec(name string, arg ...string) error {
2829
return nil
2930
}
3031

32+
// ExecAnsibleGalaxy is shortcut for executing a command via ansible-galaxy binary
3133
func ExecAnsibleGalaxy(args ...string) error {
3234
collectionDir, err := ansible.GetCollectionDirs()
3335
if err != nil {
@@ -37,6 +39,7 @@ func ExecAnsibleGalaxy(args ...string) error {
3739
return Exec("ansible-galaxy", args...)
3840
}
3941

42+
// ExecAnsiblePlaybook is shortcut for executing a playbook within the StackHead collection via ansible-playbook binary
4043
func ExecAnsiblePlaybook(playbookName string, inventoryPath string) error {
4144
stackHeadLocation, err := ansible.GetStackHeadCollectionLocation()
4245
if err != nil {

routines/tasks.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,40 +11,44 @@ import (
1111
)
1212

1313
const (
14-
InfoColor = "\033[1;34m%s\033[0m%s"
15-
NoticeColor = "\033[1;36m%s\033[0m%s"
16-
WarningColor = "\033[1;33m%s\033[0m%s"
17-
ErrorColor = "\033[1;31m%s\033[0m%s"
14+
// ErrorColor colors the first text red and the second text default color
15+
ErrorColor = "\033[1;31m%s\033[0m%s"
16+
// SuccessColor colors the first text green and the second text default color
1817
SuccessColor = "\033[1;32m%s\033[0m%s"
19-
DebugColor = "\033[0;36m%s\033[0m%s"
2018
)
2119

20+
// TaskOptions is a configuration used to define the behaviour of tasks and processing functions
2221
type TaskOptions struct {
2322
Text string
2423
Execute func(wg *sync.WaitGroup, results chan TaskResult)
2524
}
2625

26+
// TaskResult is the result of a task execution and expected to be returned into the respective channel
2727
type TaskResult struct {
2828
// internal name
2929
Name string
3030
Message string
3131
Error bool
3232
}
3333

34+
// Text assigns the given text to TaskOption.Text
3435
func Text(text string) TaskOption {
3536
return func(args *TaskOptions) {
3637
args.Text = text
3738
}
3839
}
3940

41+
// Execute assigns the given execution function to TaskOption.Execute
4042
func Execute(executeFunc func(wg *sync.WaitGroup, results chan TaskResult)) TaskOption {
4143
return func(args *TaskOptions) {
4244
args.Execute = executeFunc
4345
}
4446
}
4547

48+
// TaskOption is a single task setting
4649
type TaskOption func(*TaskOptions)
4750

51+
// RunTask executes a task that can be configured using task options
4852
func RunTask(options ...TaskOption) {
4953
t := &TaskOptions{
5054
Text: "Processing...",

0 commit comments

Comments
 (0)