-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
1,256 additions
and
59 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
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,36 @@ | ||
// SPDX-FileCopyrightText: Copyright 2024 Prasad Tengse | ||
// SPDX-License-Identifier: GPLv3-only | ||
|
||
package common | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/pflag" | ||
) | ||
|
||
// AddTemplateFlags adds | ||
// - String flag --template/-t | ||
// - String flag --template-file/-T | ||
// | ||
// and marks both flags as MutuallyExclusive. | ||
func AddTemplateFlags(cmd *cobra.Command, template, templateFile *string) { | ||
cmd.Flags().StringVarP(template, "template", "t", "", "go template to render") | ||
cmd.Flags().StringVarP(templateFile, "template-file", "T", "", "go template file to render") | ||
cmd.MarkFlagsMutuallyExclusive("template", "template-file") | ||
} | ||
|
||
// AddOutputFlagsWithAppend adds | ||
// - String flag --output/-o for output file. | ||
// - Bool flag --append/-a bool flag to indicate that output file should be appended, not overwritten. | ||
// - Bool flag --append-at-newline bool flag to indicate that output should be written on a new line. | ||
func AddOutputFlagsWithAppend(cmd *cobra.Command, output *string, append, lf *bool) { | ||
cmd.Flags().StringVarP(output, "output", "o", "", "output file path") | ||
cmd.Flags().BoolVarP(append, "append", "a", false, "append to output file") | ||
cmd.Flags().BoolVar(lf, "append-at-newline", true, "write output on a newline") | ||
cmd.Flags().SetNormalizeFunc(func(_ *pflag.FlagSet, name string) pflag.NormalizedName { | ||
if name == "append-at-lf" { | ||
name = "append-at-newline" | ||
} | ||
return pflag.NormalizedName(name) | ||
}) | ||
} |
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,35 @@ | ||
// SPDX-FileCopyrightText: Copyright 2024 Prasad Tengse | ||
// SPDX-License-Identifier: GPLv3-only | ||
|
||
package common | ||
|
||
import ( | ||
"fmt" | ||
"slices" | ||
|
||
"github.com/spf13/pflag" | ||
) | ||
|
||
var _ pflag.Value = (*stringEnumFlag)(nil) | ||
|
||
// stringEnumFlag is a custom flag which restricts flag values to ones specified. | ||
type stringEnumFlag struct { | ||
allowed []string | ||
value string | ||
} | ||
|
||
func (e *stringEnumFlag) String() string { | ||
return e.value | ||
} | ||
|
||
func (e *stringEnumFlag) Type() string { | ||
return "string" | ||
} | ||
|
||
func (e *stringEnumFlag) Set(p string) error { | ||
if !slices.Contains(e.allowed, p) { | ||
return fmt.Errorf("") | ||
} | ||
e.value = p | ||
return 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
Oops, something went wrong.