Skip to content

Commit 912f222

Browse files
committed
refactor: renamed members of AppContext to remove 'Flag' redundant suffix
1 parent 5c8c861 commit 912f222

File tree

6 files changed

+43
-183
lines changed

6 files changed

+43
-183
lines changed

cmd/release.go

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"github.com/s0ders/go-semver-release/v6/internal/branch"
1515
"github.com/s0ders/go-semver-release/v6/internal/ci"
1616
"github.com/s0ders/go-semver-release/v6/internal/gpg"
17-
"github.com/s0ders/go-semver-release/v6/internal/monorepo"
1817
"github.com/s0ders/go-semver-release/v6/internal/parser"
1918
"github.com/s0ders/go-semver-release/v6/internal/remote"
2019
"github.com/s0ders/go-semver-release/v6/internal/rule"
@@ -48,12 +47,7 @@ func NewReleaseCmd(ctx *appcontext.AppContext) *cobra.Command {
4847
return fmt.Errorf("loading branches configuration: %w", err)
4948
}
5049

51-
ctx.Projects, err = configureProjects(ctx)
52-
if err != nil {
53-
return fmt.Errorf("loading projects configuration: %w", err)
54-
}
55-
56-
origin = remote.New(ctx.RemoteNameFlag, ctx.AccessTokenFlag)
50+
origin = remote.New(ctx.RemoteName, ctx.AccessToken)
5751

5852
repository, err = origin.Clone(args[0])
5953
if err != nil {
@@ -65,15 +59,15 @@ func NewReleaseCmd(ctx *appcontext.AppContext) *cobra.Command {
6559
return fmt.Errorf("computing new semver: %w", err)
6660
}
6761

68-
tagger := tag.NewTagger(ctx.GitNameFlag, ctx.GitEmailFlag, tag.WithTagPrefix(ctx.TagPrefixFlag), tag.WithSignKey(entity))
62+
tagger := tag.NewTagger(ctx.GitName, ctx.GitEmail, tag.WithTagPrefix(ctx.TagPrefix), tag.WithSignKey(entity))
6963

7064
for _, output := range outputs {
7165
semver := output.Semver
7266
release := output.NewRelease
7367
commitHash := output.CommitHash
7468
project := output.Project.Name
7569

76-
err = ci.GenerateGitHubOutput(semver, output.Branch, ci.WithNewRelease(release), ci.WithTagPrefix(ctx.TagPrefixFlag), ci.WithProject(project))
70+
err = ci.GenerateGitHubOutput(semver, output.Branch, ci.WithNewRelease(release), ci.WithTagPrefix(ctx.TagPrefix), ci.WithProject(project))
7771
if err != nil {
7872
return fmt.Errorf("generating github output: %w", err)
7973
}
@@ -92,7 +86,7 @@ func NewReleaseCmd(ctx *appcontext.AppContext) *cobra.Command {
9286
switch {
9387
case !release:
9488
logEvent.Msg("no new release")
95-
case release && ctx.DryRunFlag:
89+
case release && ctx.DryRun:
9690
logEvent.Msg("dry-run enabled, next release found")
9791
default:
9892
logEvent.Msg("new release found")
@@ -146,33 +140,16 @@ func configureBranches(ctx *appcontext.AppContext) ([]branch.Branch, error) {
146140
return unmarshalledBranches, nil
147141
}
148142

149-
func configureProjects(ctx *appcontext.AppContext) ([]monorepo.Project, error) {
150-
flag := ctx.MonorepositoryFlag
151-
152-
if flag.String() == "[]" {
153-
return nil, nil
154-
}
155-
156-
monorepoJSON := []map[string]string(flag)
157-
158-
projects, err := monorepo.Unmarshall(monorepoJSON)
159-
if err != nil {
160-
return nil, fmt.Errorf("parsing monorepository projects configuration: %w", err)
161-
}
162-
163-
return projects, nil
164-
}
165-
166143
func configureGPGKey(ctx *appcontext.AppContext) (*openpgp.Entity, error) {
167-
flag := ctx.GPGKeyPathFlag
144+
flag := ctx.GPGKeyPath
168145

169146
if flag == "" {
170147
return nil, nil
171148
}
172149

173-
ctx.Logger.Debug().Str("path", ctx.GPGKeyPathFlag).Msg("using the following armored key for signing")
150+
ctx.Logger.Debug().Str("path", ctx.GPGKeyPath).Msg("using the following armored key for signing")
174151

175-
armoredKeyFile, err := os.ReadFile(ctx.GPGKeyPathFlag)
152+
armoredKeyFile, err := os.ReadFile(ctx.GPGKeyPath)
176153
if err != nil {
177154
return nil, fmt.Errorf("reading armored key: %w", err)
178155
}

cmd/release_test.go

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
"github.com/s0ders/go-semver-release/v6/internal/appcontext"
1818
"github.com/s0ders/go-semver-release/v6/internal/branch"
1919
"github.com/s0ders/go-semver-release/v6/internal/gittest"
20-
"github.com/s0ders/go-semver-release/v6/internal/monorepo"
2120
"github.com/s0ders/go-semver-release/v6/internal/rule"
2221
"github.com/s0ders/go-semver-release/v6/internal/tag"
2322
)
@@ -46,7 +45,7 @@ func TestReleaseCmd_ConfigurationAsEnvironmentVariable(t *testing.T) {
4645
_, err = th.ExecuteCommand("release", testRepository.Path)
4746
checkErr(t, err, "executing command")
4847

49-
assert.Equal(accessToken, th.Ctx.AccessTokenFlag, "access token flag value should be equal to environment variable value")
48+
assert.Equal(accessToken, th.Ctx.AccessToken, "access token flag value should be equal to environment variable value")
5049
}
5150

5251
func TestReleaseCmd_ConfigurationAsFile(t *testing.T) {
@@ -874,16 +873,6 @@ func TestReleaseCmd_ConfigureBranches_NoBranches(t *testing.T) {
874873
assert.ErrorIs(err, branch.ErrNoBranch)
875874
}
876875

877-
func TestReleaseCmd_ConfigureProjects_NoProjects(t *testing.T) {
878-
assert := assertion.New(t)
879-
ctx := appcontext.New()
880-
881-
projects, err := configureProjects(ctx)
882-
checkErr(t, err, "configuring projects")
883-
884-
assert.Nil(projects, "no monorepo configuration, should have gotten nil")
885-
}
886-
887876
func TestReleaseCmd_InvalidCustomRules(t *testing.T) {
888877
assert := assertion.New(t)
889878
ctx := appcontext.New()
@@ -907,21 +896,11 @@ func TestReleaseCmd_InvalidBranch(t *testing.T) {
907896
assert.ErrorIs(err, branch.ErrNoName, "should have failed parsing branch with no name")
908897
}
909898

910-
func TestReleaseCmd_InvalidMonorepoProjects(t *testing.T) {
911-
assert := assertion.New(t)
912-
ctx := appcontext.New()
913-
914-
ctx.MonorepositoryFlag = []map[string]string{{"path": "foo"}}
915-
916-
_, err := configureProjects(ctx)
917-
assert.ErrorIs(err, monorepo.ErrNoName, "should have failed parsing project with no name")
918-
}
919-
920899
func TestReleaseCmd_InvalidArmoredKeyPath(t *testing.T) {
921900
assert := assertion.New(t)
922901
ctx := appcontext.New()
923902

924-
ctx.GPGKeyPathFlag = "./does/not/exist"
903+
ctx.GPGKeyPath = "./does/not/exist"
925904

926905
_, err := configureGPGKey(ctx)
927906

@@ -958,7 +937,7 @@ func TestReleaseCmd_InvalidArmoredKeyContent(t *testing.T) {
958937
}
959938
}()
960939

961-
ctx.GPGKeyPathFlag = keyFilePath
940+
ctx.GPGKeyPath = keyFilePath
962941

963942
_, err = configureGPGKey(ctx)
964943
assert.ErrorContains(err, "loading armored key", "should have failed trying to read armored key ring from empty file")

cmd/root.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func NewRootCommand(ctx *appcontext.AppContext) *cobra.Command {
4444
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
4545
ctx.Logger = zerolog.New(cmd.OutOrStdout()).Level(zerolog.InfoLevel)
4646

47-
if ctx.VerboseFlag {
47+
if ctx.Verbose {
4848
ctx.Logger = ctx.Logger.Level(zerolog.DebugLevel)
4949
}
5050

@@ -53,19 +53,19 @@ func NewRootCommand(ctx *appcontext.AppContext) *cobra.Command {
5353
TraverseChildren: true,
5454
}
5555

56-
rootCmd.PersistentFlags().StringVar(&ctx.AccessTokenFlag, AccessTokenConfiguration, "", "Access token used to push tag to Git remote")
56+
rootCmd.PersistentFlags().StringVar(&ctx.AccessToken, AccessTokenConfiguration, "", "Access token used to push tag to Git remote")
5757
rootCmd.PersistentFlags().VarP(&ctx.BranchesFlag, BranchesConfiguration, "b", "An array of branches such as [{\"name\": \"main\"}, {\"name\": \"rc\", \"prerelease\": true}]")
58-
rootCmd.PersistentFlags().StringVar(&ctx.BuildMetadataFlag, BuildMetadataConfiguration, "", "Build metadata (e.g. build number) that will be appended to the SemVer")
59-
rootCmd.PersistentFlags().StringVar(&ctx.CfgFileFlag, "config", "", "Configuration file path (default \"./"+defaultConfigFile+"."+configFileFormat+"\")")
60-
rootCmd.PersistentFlags().BoolVarP(&ctx.DryRunFlag, DryRunConfiguration, "d", false, "Only compute the next SemVer, do not push any tag")
61-
rootCmd.PersistentFlags().StringVar(&ctx.GitEmailFlag, GitEmailConfiguration, "go-semver@release.ci", "Email used in semantic version tags")
62-
rootCmd.PersistentFlags().StringVar(&ctx.GitNameFlag, GitNameConfiguration, "Go Semver Release", "Name used in semantic version tags")
63-
rootCmd.PersistentFlags().StringVar(&ctx.GPGKeyPathFlag, GPGPathConfiguration, "", "Path to an armored GPG key used to sign produced tags")
64-
rootCmd.PersistentFlags().Var(&ctx.MonorepositoryFlag, MonorepoConfiguration, "An array of branches such as [{\"name\": \"foo\", \"path\": \"./foo/\"}]")
65-
rootCmd.PersistentFlags().StringVar(&ctx.RemoteNameFlag, RemoteNameConfiguration, "origin", "Name of the Git repository remote")
58+
rootCmd.PersistentFlags().StringVar(&ctx.BuildMetadata, BuildMetadataConfiguration, "", "Build metadata (e.g. build number) that will be appended to the SemVer")
59+
rootCmd.PersistentFlags().StringVar(&ctx.CfgFile, "config", "", "Configuration file path (default \"./"+defaultConfigFile+"."+configFileFormat+"\")")
60+
rootCmd.PersistentFlags().BoolVarP(&ctx.DryRun, DryRunConfiguration, "d", false, "Only compute the next SemVer, do not push any tag")
61+
rootCmd.PersistentFlags().StringVar(&ctx.GitEmail, GitEmailConfiguration, "go-semver@release.ci", "Email used in semantic version tags")
62+
rootCmd.PersistentFlags().StringVar(&ctx.GitName, GitNameConfiguration, "Go Semver Release", "Name used in semantic version tags")
63+
rootCmd.PersistentFlags().StringVar(&ctx.GPGKeyPath, GPGPathConfiguration, "", "Path to an armored GPG key used to sign produced tags")
64+
rootCmd.PersistentFlags().Var(&ctx.MonorepositoryCfg, MonorepoConfiguration, "An array of branches such as [{\"name\": \"foo\", \"path\": \"./foo/\"}]")
65+
rootCmd.PersistentFlags().StringVar(&ctx.RemoteName, RemoteNameConfiguration, "origin", "Name of the Git repository remote")
6666
rootCmd.PersistentFlags().Var(&ctx.RulesFlag, RulesConfiguration, "A hashmap of array such as {\"minor\": [\"feat\"], \"patch\": [\"fix\", \"perf\"]} ]")
67-
rootCmd.PersistentFlags().StringVar(&ctx.TagPrefixFlag, TagPrefixConfiguration, "v", "Prefix added to the version tag name")
68-
rootCmd.PersistentFlags().BoolVarP(&ctx.VerboseFlag, "verbose", "v", false, "Verbose output")
67+
rootCmd.PersistentFlags().StringVar(&ctx.TagPrefix, TagPrefixConfiguration, "v", "Prefix added to the version tag name")
68+
rootCmd.PersistentFlags().BoolVarP(&ctx.Verbose, "verbose", "v", false, "Verbose output")
6969

7070
releaseCmd := NewReleaseCmd(ctx)
7171
versionCmd := NewVersionCmd()
@@ -77,15 +77,15 @@ func NewRootCommand(ctx *appcontext.AppContext) *cobra.Command {
7777
}
7878

7979
func initializeConfig(cmd *cobra.Command, ctx *appcontext.AppContext) error {
80-
if ctx.CfgFileFlag != "" {
81-
ctx.Viper.SetConfigFile(ctx.CfgFileFlag)
80+
if ctx.CfgFile != "" {
81+
ctx.Viper.SetConfigFile(ctx.CfgFile)
8282
} else {
8383
ctx.Viper.AddConfigPath(".")
8484
ctx.Viper.SetConfigType(configFileFormat)
8585
ctx.Viper.SetConfigName(defaultConfigFile)
8686
}
8787

88-
absCfgPath, err := filepath.Abs(ctx.CfgFileFlag)
88+
absCfgPath, err := filepath.Abs(ctx.CfgFile)
8989
if err != nil {
9090
return fmt.Errorf("getting configuration file absolute path: %w", err)
9191
}

internal/appcontext/app_context.go

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Package appcontext provides a structure to store the current application execution context.
22
//
3-
// The use of this structure allows to avoid the use of global variables to share the states of variables across
3+
// The use of this structure allows avoiding the use of global variables to share the states of variables across
44
// structures and functions.
55
package appcontext
66

@@ -14,24 +14,23 @@ import (
1414
)
1515

1616
type AppContext struct {
17-
Viper *viper.Viper
18-
Branches []branch.Branch
19-
Projects []monorepo.Project
20-
Rules rule.Rules
21-
BranchesFlag branch.Flag
22-
MonorepositoryFlag monorepo.Flag
23-
RulesFlag rule.Flag
24-
Logger zerolog.Logger
25-
CfgFileFlag string
26-
GitNameFlag string
27-
GitEmailFlag string
28-
TagPrefixFlag string
29-
AccessTokenFlag string
30-
RemoteNameFlag string
31-
GPGKeyPathFlag string
32-
BuildMetadataFlag string
33-
DryRunFlag bool
34-
VerboseFlag bool
17+
Viper *viper.Viper
18+
Branches []branch.Branch
19+
Rules rule.Rules
20+
BranchesFlag branch.Flag
21+
MonorepositoryCfg monorepo.Flag
22+
RulesFlag rule.Flag
23+
Logger zerolog.Logger
24+
CfgFile string
25+
GitName string
26+
GitEmail string
27+
TagPrefix string
28+
AccessToken string
29+
RemoteName string
30+
GPGKeyPath string
31+
BuildMetadata string
32+
DryRun bool
33+
Verbose bool
3534
}
3635

3736
func New() *AppContext {

internal/monorepo/flag_test.go

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

internal/monorepo/monorepo_test.go

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

0 commit comments

Comments
 (0)