Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions .github/workflows/go-coverage-report.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
name: CI

# This setup assumes that you run the unit tests with code coverage in the same
# workflow that will also print the coverage report as comment to the pull request.
# Therefore, you need to trigger this workflow when a pull request is (re)opened or
# when new code is pushed to the branch of the pull request. In addition, you also
# need to trigger this workflow when new code is pushed to the main branch because
# we need to upload the code coverage results as artifact for the main branch as
# well since it will be the baseline code coverage.
#
# We do not want to trigger the workflow for pushes to *any* branch because this
# would trigger our jobs twice on pull requests (once from "push" event and once
# from "pull_request->synchronize")
on:
pull_request:
types: [opened, reopened, synchronize]
push:
branches:
- 'main'

jobs:
unit_tests:
name: "Unit tests"
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Setup Go
uses: actions/setup-go@v4
with:
go-version: ^1.22

# When you execute your unit tests, make sure to use the "-coverprofile" flag to write a
# coverage profile to a file. You will need the name of the file (e.g. "coverage.txt")
# in the next step as well as the next job.
- name: pwd
run: pwd && ls -laf

- name: Test
run: |
cd bsctl
go test -cover -coverprofile=coverage.txt ./...

- name: Archive code coverage results
uses: actions/upload-artifact@v4
with:
name: code-coverage
path: bsctl/coverage.txt # Make sure to use the same file name you chose for the "-coverprofile" in the "Test" step

code_coverage:
name: "Code coverage report"
if: github.event_name == 'pull_request' # Do not run when workflow is triggered by push to main branch
runs-on: ubuntu-latest
needs: unit_tests # Depends on the artifact uploaded by the "unit_tests" job
steps:
- uses: fgrosse/go-coverage-report@v1.0.1 # Consider using a Git revision for maximum security
with:
coverage-artifact-name: "code-coverage" # can be omitted if you used this default value
coverage-file-name: "coverage.txt" # can be omitted if you used this default value
root-package: "github.com/mrlunchbox777/basic-setup/bsctl/main.go"
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ install/minikube*
!/shared-scripts/big-bang/bin/.gitkeep
test.out
bin/
coverage.txt
42 changes: 42 additions & 0 deletions bsctl/cmd/basic_setup/add_general_rc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package basic_setup

import (
"fmt"

"github.com/spf13/cobra"
genericIOOptions "k8s.io/cli-runtime/pkg/genericiooptions"
cmdUtil "k8s.io/kubectl/pkg/cmd/util"
"k8s.io/kubectl/pkg/util/i18n"
"k8s.io/kubectl/pkg/util/templates"

bsUtil "github.com/mrlunchbox777/basic-setup/bsctl/util"
)

var (
addGeneralRcUse = `add-general-rc`

addGeneralRcShort = i18n.T(`Add general rc to ~/.*rc.`)

addGeneralRcLong = templates.LongDesc(i18n.T(`Ensure that general rc is added to ~/.*rc. That is, ensure that the general rc is added to the user's rc file.`))

addGeneralRcExample = templates.Examples(i18n.T(`
# Add general rc to ~/.*rc
bsctl basic-setup add-general-rc
`))
)

// NewAddGeneralRcCmd - new add-general-rc command
func NewAddGeneralRcCmd(factory bsUtil.Factory, streams genericIOOptions.IOStreams) *cobra.Command {
cmd := &cobra.Command{
Use: addGeneralRcUse,
Short: addGeneralRcShort,
Long: addGeneralRcLong,
Example: addGeneralRcExample,
Run: func(cmd *cobra.Command, args []string) {
_, err := streams.Out.Write([]byte(fmt.Sprintln("Please provide a subcommand for basic-setup (see help)")))
cmdUtil.CheckErr(err)
},
}

return cmd
}
44 changes: 44 additions & 0 deletions bsctl/cmd/basic_setup/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package basic_setup

import (
"fmt"

"github.com/spf13/cobra"
genericIOOptions "k8s.io/cli-runtime/pkg/genericiooptions"
cmdUtil "k8s.io/kubectl/pkg/cmd/util"
"k8s.io/kubectl/pkg/util/i18n"
"k8s.io/kubectl/pkg/util/templates"

bsUtil "github.com/mrlunchbox777/basic-setup/bsctl/util"
)

var (
rootUse = `basic-setup`

rootShort = i18n.T(`Manage and invoke basic setup functionality.`)

rootLong = templates.LongDesc(i18n.T(`Manage and invoke basic setup functionality.`))

rootExample = templates.Examples(i18n.T(`
# Print basic setup help
bsctl basic-setup -h
`))
)

// NewBasicSetupCmd - new basic-setup command
func NewBasicSetupCmd(factory bsUtil.Factory, streams genericIOOptions.IOStreams) *cobra.Command {
cmd := &cobra.Command{
Use: rootUse,
Short: rootShort,
Long: rootLong,
Example: rootExample,
Run: func(cmd *cobra.Command, args []string) {
_, err := streams.Out.Write([]byte(fmt.Sprintln("Please provide a subcommand for basic-setup (see help)")))
cmdUtil.CheckErr(err)
},
}

cmd.AddCommand(NewAddGeneralRcCmd(factory, streams))

return cmd
}
41 changes: 41 additions & 0 deletions bsctl/cmd/basic_setup/root_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package basic_setup

import (
"testing"

"github.com/stretchr/testify/assert"
genericIOOptions "k8s.io/cli-runtime/pkg/genericiooptions"

bbTestUtil "github.com/mrlunchbox777/basic-setup/bsctl/util/test"
)

func TestBasicSetup_RootUsage(t *testing.T) {
// Arrange
streams, _, _, _ := genericIOOptions.NewTestIOStreams()
factory := bbTestUtil.GetFakeFactory()
// Act
cmd := NewBasicSetupCmd(factory, streams)
// Assert
assert.NotNil(t, cmd)
assert.Equal(t, "basic-setup", cmd.Use)
commandsList := cmd.Commands()
assert.Len(t, commandsList, 1)
var commandUseNamesList []string
for _, command := range commandsList {
commandUseNamesList = append(commandUseNamesList, command.Use)
}
assert.Contains(t, commandUseNamesList, "add-general-rc")
}

func TestK3d_RootNoSubcommand(t *testing.T) {
// Arrange
streams, in, out, errout := genericIOOptions.NewTestIOStreams()
factory := bbTestUtil.GetFakeFactory()
// Act
cmd := NewBasicSetupCmd(factory, streams)
// Assert
assert.Nil(t, cmd.Execute())
assert.Empty(t, in.String())
assert.Empty(t, errout.String())
assert.Contains(t, out.String(), "Please provide a subcommand for basic-setup (see help)")
}
3 changes: 3 additions & 0 deletions bsctl/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"k8s.io/kubectl/pkg/util/i18n"
"k8s.io/kubectl/pkg/util/templates"

"github.com/mrlunchbox777/basic-setup/bsctl/cmd/basic_setup"
bsUtil "github.com/mrlunchbox777/basic-setup/bsctl/util"
)

Expand Down Expand Up @@ -41,6 +42,8 @@ func NewRootCmd(factory bsUtil.Factory, streams genericIOOptions.IOStreams) *cob
cmd.AddCommand(NewCompletionCmd(factory, streams))
cmd.AddCommand(NewVersionCmd(factory, streams))

cmd.AddCommand(basic_setup.NewBasicSetupCmd(factory, streams))

addHelpCommandsRecursively(cmd)

return cmd
Expand Down
5 changes: 0 additions & 5 deletions bsctl/cmd/constants.go

This file was deleted.

8 changes: 6 additions & 2 deletions bsctl/cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"k8s.io/kubectl/pkg/util/i18n"
"k8s.io/kubectl/pkg/util/templates"

"github.com/mrlunchbox777/basic-setup/bsctl/static"
bsUtil "github.com/mrlunchbox777/basic-setup/bsctl/util"
)

Expand Down Expand Up @@ -40,9 +41,12 @@ func NewVersionCmd(factory bsUtil.Factory, streams genericIOOptions.IOStreams) *
return cmd
}

// query the cluster using helm module to get information on bigbang release
func bsVersion(streams genericIOOptions.IOStreams) error {
fmt.Fprintf(streams.Out, "basic-setup cli version %s\n", BasicSetupCliVersion)
constants, err := static.GetConstants()
if err != nil {
return err
}
fmt.Fprintf(streams.Out, "basic-setup cli version %s\n", constants.BasicSetupCliVersion)

return nil
}
8 changes: 7 additions & 1 deletion bsctl/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/mrlunchbox777/basic-setup/bsctl
go 1.22.0

require (
github.com/go-playground/validator/v10 v10.19.0
github.com/spf13/cobra v1.8.0
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.18.2
Expand All @@ -11,6 +12,11 @@ require (
)

require (
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
golang.org/x/crypto v0.21.0 // indirect
k8s.io/api v0.30.1 // indirect
k8s.io/apimachinery v0.30.1 // indirect
k8s.io/client-go v0.30.1 // indirect
Expand Down Expand Up @@ -84,7 +90,7 @@ require (
gopkg.in/evanphx/json-patch.v5 v5.8.1 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/cli-runtime v0.30.1
k8s.io/component-base v0.30.1 // indirect
Expand Down
14 changes: 14 additions & 0 deletions bsctl/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHk
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
github.com/go-errors/errors v1.5.1 h1:ZwEMSLRCapFLflTpT7NKaAc7ukJ8ZPEjzlxt8rPN8bk=
github.com/go-errors/errors v1.5.1/go.mod h1:sIVyrIiJhuEF+Pj9Ebtd6P/rEYROXFi3BopGUQ5a5Og=
github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ=
Expand All @@ -35,6 +37,14 @@ github.com/go-openapi/jsonreference v0.20.4 h1:bKlDxQxQJgwpUSgOENiMPzCTBVuc7vTdX
github.com/go-openapi/jsonreference v0.20.4/go.mod h1:5pZJyJP2MnYCpoeoMAql78cCHauHj0V9Lhc506VOpw4=
github.com/go-openapi/swag v0.22.7 h1:JWrc1uc/P9cSomxfnsFSVWoE1FW6bNbrVPmpQYpCcR8=
github.com/go-openapi/swag v0.22.7/go.mod h1:Gl91UqO+btAM0plGGxHqJcQZ1ZTy6jbmridBTsDy8A0=
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.19.0 h1:ol+5Fu+cSq9JD7SoSqe04GMI92cbn0+wvQ3bZ8b/AU4=
github.com/go-playground/validator/v10 v10.19.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI=
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
Expand Down Expand Up @@ -81,6 +91,8 @@ github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de h1:9TO3cAIGXtEhnIaL+V+BEER86oLrvS+kWobKpbJuye0=
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de/go.mod h1:zAbeS9B/r2mtpb6U+EI2rYA5OAXxsYw6wTamcNW+zcE=
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
Expand Down Expand Up @@ -171,6 +183,8 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA=
golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs=
golang.org/x/exp v0.0.0-20240119083558-1b970713d09a h1:Q8/wZp0KX97QFTc2ywcOE0YRjZPVIx+MXInMzdvQqcA=
golang.org/x/exp v0.0.0-20240119083558-1b970713d09a/go.mod h1:idGWGoKP1toJGkd5/ig9ZLuPcZBC3ewk7SzmH0uou08=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
Expand Down
45 changes: 45 additions & 0 deletions bsctl/static/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package static

import (
"embed"

yaml "gopkg.in/yaml.v2"
)

var (
//go:embed resources
resources embed.FS
constants = Constants{}
)

func GetConstants() (Constants, error) {
constants.readFileFunc = resources.ReadFile
err := constants.readConstants()
return constants, err
}

type Readable interface {
readConstants() error
}

type ReadFileFunc func(string) ([]byte, error)

func (r ReadFileFunc) ReadFile(s string) ([]byte, error) {
return r(s)
}

type Constants struct {
// readFileFunc - function to read file
readFileFunc ReadFileFunc
// BasicSetupCliVersion - constance for sematic versioning
BasicSetupCliVersion string `yaml:"BasicSetupCliVersion"`
}

func (c *Constants) readConstants() error {
yamlFile, err := c.readFileFunc.ReadFile("resources/constants.yaml")
if err != nil {
return err
}
err = yaml.Unmarshal(yamlFile, c)
return err
}
37 changes: 37 additions & 0 deletions bsctl/static/constants_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package static

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestReadConstants(t *testing.T) {
// Arrange & Act
c, err := GetConstants()
// Assert
assert.Nil(t, err)
assert.NotNil(t, c)
}

func TestAssertConstants(t *testing.T) {
// Arrange & Act
c, err := GetConstants()
// Assert
assert.Nil(t, err)
assert.NotNil(t, c)
assert.Equal(t, "0.1.0", c.BasicSetupCliVersion)
}

func TestErrorConstants(t *testing.T) {
// Arrange
c, err := GetConstants()
assert.Nil(t, err)
// Act
c.readFileFunc = func(s string) ([]byte, error) {
return nil, assert.AnError
}
err = c.readConstants()
// Assert
assert.NotNil(t, err)
}
Loading