Skip to content

Commit f762b83

Browse files
authored
Fix/cobra init (#29)
* fix: cobra simplified * fix: remove duplicate workflow * fix: add more tests * fix: add more tests change logger package * fix: add writer/reader impl in cmd callers * fix: all tests and implementations updated * fix: parallelise tests * fix: remove duplicates * fix: add taskfile remove makefile
1 parent 7d4f48d commit f762b83

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1078
-1136
lines changed

.github/workflows/build.yml

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ name: CI
33
on:
44
push:
55
branches: [ master, main ]
6+
pull_request:
7+
branches: [ master, main ]
68

79
jobs:
810
set-version:
@@ -22,11 +24,11 @@ jobs:
2224
git config user.email ${{ github.actor }}[email protected]
2325
git config user.name ${{ github.actor }}
2426
- name: Install GitVersion
25-
uses: gittools/actions/gitversion/setup@v1.1.1
27+
uses: gittools/actions/gitversion/setup@v3.0.0
2628
with:
2729
versionSpec: '5.x'
2830
- name: Set SemVer Version
29-
uses: gittools/actions/gitversion/execute@v1.1.1
31+
uses: gittools/actions/gitversion/execute@v3.0.0
3032
id: gitversion
3133

3234
- name: echo VERSIONS
@@ -36,7 +38,7 @@ jobs:
3638
test:
3739
runs-on: ubuntu-latest
3840
container:
39-
image: golang:1.21-bullseye
41+
image: golang:1.23-bullseye
4042
needs: set-version
4143
env:
4244
SEMVER: ${{ needs.set-version.outputs.semVer }}
@@ -46,15 +48,24 @@ jobs:
4648
- uses: actions/checkout@v4
4749
with:
4850
fetch-depth: 1
51+
- name: Install Task
52+
uses: arduino/setup-task@v2
53+
with:
54+
version: 3.x
55+
repo-token: ${{ secrets.GITHUB_TOKEN }}
56+
4957
- name: install deps
5058
run: |
5159
apt update && apt install -y jq git
5260
git config --global --add safe.directory "$GITHUB_WORKSPACE"
5361
git config user.email ${{ github.actor }}[email protected]
5462
git config user.name ${{ github.actor }}
55-
- name: make test
63+
- name: Run Lint
64+
run: |
65+
task lint
66+
- name: Run Tests
5667
run: |
57-
make REVISION=$GITHUB_SHA test
68+
task coverage
5869
- name: Publish Junit style Test Report
5970
uses: mikepenz/action-junit-report@v4
6071
if: always() # always run even if the previous step fails

.github/workflows/pr.yml

Lines changed: 0 additions & 65 deletions
This file was deleted.

.github/workflows/release.yml

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@ on:
55
workflows: ['CI']
66
types:
77
- completed
8+
branches:
9+
- master
10+
- main
811

912
jobs:
1013
set-version:
1114
runs-on: ubuntu-latest
12-
if: ${{ github.event.workflow_run.conclusion == 'success' }}
15+
if: ${{ github.event.workflow_run.head_branch == 'master' && github.event.workflow_run.conclusion == 'success' }}
1316
container:
1417
image: mcr.microsoft.com/dotnet/sdk:6.0
1518
outputs:
@@ -25,21 +28,17 @@ jobs:
2528
git config user.email ${{ github.actor }}[email protected]
2629
git config user.name ${{ github.actor }}
2730
- name: Install GitVersion
28-
uses: gittools/actions/gitversion/setup@v1.1.1
31+
uses: gittools/actions/gitversion/setup@v3.0.0
2932
with:
3033
versionSpec: '5.x'
3134
- name: Set SemVer Version
32-
uses: gittools/actions/gitversion/execute@v1.1.1
35+
uses: gittools/actions/gitversion/execute@v3.0.0
3336
id: gitversion
3437

35-
- name: echo VERSIONS
36-
run: |
37-
echo "REVISION -> $GITHUB_SHA"
38-
echo "VERSION -> $GITVERSION_SEMVER"
3938
release:
4039
runs-on: ubuntu-latest
4140
container:
42-
image: golang:1.21-bullseye
41+
image: golang:1.23-bullseye
4342
env:
4443
FOO: Bar
4544
needs: set-version
@@ -49,6 +48,13 @@ jobs:
4948
- uses: actions/checkout@v4
5049
with:
5150
fetch-depth: 1
51+
52+
- name: Install Task
53+
uses: arduino/setup-task@v2
54+
with:
55+
version: 3.x
56+
repo-token: ${{ secrets.GITHUB_TOKEN }}
57+
5258
- name: install deps
5359
run: |
5460
apt-get update && apt-get install jq git -y
@@ -57,8 +63,8 @@ jobs:
5763
git config user.name ${{ github.actor }}
5864
- name: release library
5965
run: |
60-
make GIT_TAG=${SEMVER} REVISION=$GITHUB_SHA tag
66+
task tag GIT_TAG=${SEMVER} REVISION=$GITHUB_SHA
6167
- name: release binary
6268
run: |
63-
make REVISION=$GITHUB_SHA GIT_TAG=${SEMVER} PAT=${{ secrets.GITHUB_TOKEN }} cross-build
64-
make REVISION=$GITHUB_SHA GIT_TAG=${SEMVER} PAT=${{ secrets.GITHUB_TOKEN }} release
69+
task bin REVISION=$GITHUB_SHA GIT_TAG=${SEMVER}
70+
task release REVISION=$GITHUB_SHA GIT_TAG=${SEMVER} PAT=${{ secrets.GITHUB_TOKEN }}

Makefile

Lines changed: 0 additions & 58 deletions
This file was deleted.

cmd/configmanager/configmanager.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package cmd
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"io"
7+
8+
"github.com/dnitsch/configmanager"
9+
"github.com/dnitsch/configmanager/internal/cmdutils"
10+
"github.com/dnitsch/configmanager/internal/config"
11+
"github.com/dnitsch/configmanager/pkg/generator"
12+
"github.com/dnitsch/configmanager/pkg/log"
13+
"github.com/spf13/cobra"
14+
)
15+
16+
var (
17+
Version string = "0.0.1"
18+
Revision string = "1111aaaa"
19+
)
20+
21+
type rootCmdFlags struct {
22+
verbose bool
23+
tokenSeparator string
24+
keySeparator string
25+
}
26+
27+
type Root struct {
28+
Cmd *cobra.Command
29+
logger log.ILogger
30+
rootFlags *rootCmdFlags
31+
}
32+
33+
func NewRootCmd(logger log.ILogger) *Root { //channelOut, channelErr io.Writer
34+
rc := &Root{
35+
Cmd: &cobra.Command{
36+
Use: config.SELF_NAME,
37+
Short: fmt.Sprintf("%s CLI for retrieving and inserting config or secret variables", config.SELF_NAME),
38+
Long: fmt.Sprintf(`%s CLI for retrieving config or secret variables.
39+
Using a specific tokens as an array item`, config.SELF_NAME),
40+
SilenceUsage: true,
41+
Version: fmt.Sprintf("%s-%s", Version, Revision),
42+
},
43+
logger: logger,
44+
rootFlags: &rootCmdFlags{},
45+
}
46+
47+
rc.Cmd.PersistentFlags().BoolVarP(&rc.rootFlags.verbose, "verbose", "v", false, "Verbosity level")
48+
rc.Cmd.PersistentFlags().StringVarP(&rc.rootFlags.tokenSeparator, "token-separator", "s", "#", "Separator to use to mark concrete store and the key within it")
49+
rc.Cmd.PersistentFlags().StringVarP(&rc.rootFlags.keySeparator, "key-separator", "k", "|", "Separator to use to mark a key look up in a map. e.g. AWSSECRETS#/token/map|key1")
50+
addSubCmds(rc)
51+
return rc
52+
}
53+
54+
// addSubCmds assigns the subcommands to the parent/root command
55+
func addSubCmds(rootCmd *Root) {
56+
newFromStrCmd(rootCmd)
57+
newRetrieveCmd(rootCmd)
58+
newInsertCmd(rootCmd)
59+
}
60+
61+
func (rc *Root) Execute(ctx context.Context) error {
62+
return rc.Cmd.ExecuteContext(ctx)
63+
}
64+
65+
func cmdutilsInit(rootCmd *Root, cmd *cobra.Command, path string) (*cmdutils.CmdUtils, io.WriteCloser, error) {
66+
67+
outputWriter, err := cmdutils.GetWriter(path)
68+
if err != nil {
69+
return nil, nil, err
70+
}
71+
72+
cm := configmanager.New(cmd.Context())
73+
cm.Config.WithTokenSeparator(rootCmd.rootFlags.tokenSeparator).WithOutputPath(path).WithKeySeparator(rootCmd.rootFlags.keySeparator)
74+
gnrtr := generator.NewGenerator(cmd.Context(), func(gv *generator.GenVars) {
75+
if rootCmd.rootFlags.verbose {
76+
rootCmd.logger.SetLevel(log.DebugLvl)
77+
}
78+
gv.Logger = rootCmd.logger
79+
}).WithConfig(cm.Config)
80+
cm.WithGenerator(gnrtr)
81+
return cmdutils.New(cm, rootCmd.logger, outputWriter), outputWriter, nil
82+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package cmd_test
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"log/slog"
7+
"os"
8+
"strings"
9+
"testing"
10+
11+
cmd "github.com/dnitsch/configmanager/cmd/configmanager"
12+
"github.com/dnitsch/configmanager/pkg/log"
13+
)
14+
15+
type cmdTestInput struct {
16+
args []string
17+
errored bool
18+
exactOutput string
19+
output []string
20+
logLevel slog.Level // 8 for error -4 debug, 0 for info
21+
}
22+
type levelSetter int
23+
24+
func (l levelSetter) Level() int {
25+
return 8
26+
}
27+
func cmdRunTestHelper(t *testing.T, testInput *cmdTestInput) {
28+
t.Helper()
29+
30+
leveler := &slog.LevelVar{}
31+
leveler.Set(testInput.logLevel)
32+
33+
logErr := &bytes.Buffer{}
34+
logger := log.New(logErr) //slog.New(slog.NewTextHandler(logErr, &slog.HandlerOptions{Level: leveler}))
35+
cmd := cmd.NewRootCmd(logger)
36+
os.Args = append([]string{os.Args[0]}, testInput.args...)
37+
errOut := &bytes.Buffer{}
38+
stdOut := &bytes.Buffer{}
39+
cmd.Cmd.SetArgs(testInput.args)
40+
cmd.Cmd.SetErr(errOut)
41+
cmd.Cmd.SetOut(stdOut)
42+
43+
if err := cmd.Execute(context.TODO()); err != nil {
44+
if testInput.errored {
45+
return
46+
}
47+
t.Fatalf("\ngot: %v\nwanted <nil>\n", err)
48+
}
49+
50+
if testInput.errored && errOut.Len() < 1 {
51+
t.Errorf("\ngot: nil\nwanted an error to be thrown")
52+
}
53+
if len(testInput.output) > 0 {
54+
for _, v := range testInput.output {
55+
if !strings.Contains(stdOut.String(), v) {
56+
t.Errorf("\ngot: %s\vnot found in: %v", stdOut.String(), v)
57+
}
58+
}
59+
}
60+
if testInput.exactOutput != "" && stdOut.String() != testInput.exactOutput {
61+
t.Errorf("output mismatch\ngot: %s\n\nwanted: %s", stdOut.String(), testInput.exactOutput)
62+
}
63+
}

0 commit comments

Comments
 (0)