Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stud 3115 #1975

Closed
wants to merge 30 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
9224a9a
Rename packages to use OneCloud
bmeyers22 Feb 22, 2019
bcc54ae
Custom generator that will generate arguments and flags right from th…
bmeyers22 Feb 22, 2019
b3a45b8
Allow both persistent and local flags for root vs command. Removed de…
bmeyers22 Feb 22, 2019
65c085b
Merge pull request #1 from OneCloudInc/feature-oc-generator
bmeyers22 Feb 22, 2019
4b2965f
Fixed tests and added dep
bmeyers22 Feb 22, 2019
fe83a62
Merge pull request #4 from OneCloudInc/release-0.0.4
bmeyers22 Feb 22, 2019
8b91f1a
Merge tag '0.0.4' into develop
bmeyers22 Feb 22, 2019
96868d8
Only change the CLI tool to use OneCloudInc
bmeyers22 Feb 22, 2019
e4569a2
Merge pull request #5 from OneCloudInc/release-0.0.5
bmeyers22 Feb 22, 2019
b549b4c
Merge tag '0.0.5' into develop
bmeyers22 Feb 22, 2019
5cecced
Use RunE
bmeyers22 Feb 22, 2019
01e6d77
Merge pull request #6 from OneCloudInc/release-0.0.6
bmeyers22 Feb 22, 2019
12d6bd8
Merge tag '0.0.6' into develop
bmeyers22 Feb 22, 2019
ce89c5a
Placeholder for performing decryption logic; cobra now distributed us…
ryancurtin Dec 7, 2021
93ca442
Adds the ability to decrypt arguments using the reaper service
ryancurtin Dec 14, 2021
f927bb8
Adds test for reaper decryptor
ryancurtin Dec 14, 2021
b430be0
Ensures that payload is built correctly
ryancurtin Dec 14, 2021
2a36c3f
Explicitly sending JSON request
ryancurtin Dec 16, 2021
c21dc0f
Fixes CircleCI
ryancurtin Dec 16, 2021
ef632ed
Converting cobra to use OneCloudInc/cobra in all cases
ryancurtin Dec 16, 2021
1223d49
Fixes issue references
ryancurtin Dec 16, 2021
1f5bfa1
Code review fixes - seeding random number generator and handling erro…
ryancurtin Dec 16, 2021
18ca9a4
Merge pull request #7 from OneCloudInc/hotfix-0.1.0
ryancurtin Dec 16, 2021
6163b0a
Merge tag '0.1.0' into develop
ryancurtin Dec 16, 2021
061ea08
Merge branch 'release-1.4.0'
ryancurtin Dec 20, 2021
0f1cf0c
Merge tag 'v1.4.0' into develop
ryancurtin Dec 20, 2021
4e75f5d
Returns an early error if decryption fails
ryancurtin Apr 18, 2022
5bad1b6
Merge tag 'v1.4.1' into develop
ryancurtin Apr 18, 2022
fac4304
STUD-3115, TestUpload1
waynepaffhausen-wk Jun 8, 2023
b7d6863
Test1
waynepaffhausen-wk Jun 8, 2023
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
65 changes: 21 additions & 44 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,53 +1,30 @@
version: 2

references:
workspace: &workspace
/go/src/github.com/spf13/cobra

run_tests: &run_tests
run:
name: "All Commands"
command: |
mkdir -p bin
curl -Lso bin/shellcheck https://github.com/caarlos0/shellcheck-docker/releases/download/v0.4.6/shellcheck
chmod +x bin/shellcheck
go get -t -v ./...
PATH=$PATH:$PWD/bin go test -v ./...
go build
if [ -z $NOVET ]; then
diff -u <(echo -n) <(go tool vet . 2>&1 | grep -vE 'ExampleCommand|bash_completions.*Fprint');
fi

jobs:
go-current:
build:
docker:
- image: circleci/golang:1.11
working_directory: *workspace
# specify the version
- image: circleci/golang:1.15
auth:
username: $DOCKERHUB_USER
password: $DOCKERHUB_PASSWORD
environment: # environment variables for primary container
DEPLOYMENT: test

#### TEMPLATE_NOTE: go expects specific checkout path representing url
#### expecting it in the form of
#### /go/src/github.com/circleci/go-tool
#### /go/src/bitbucket.org/circleci/go-tool
working_directory: /go/src/github.com/OneCloudInc/cobra
steps:
- checkout
- *run_tests
- run:
name: "Check formatting"
command: diff -u <(echo -n) <(gofmt -d -s .)
go-previous:
docker:
- image: circleci/golang:1.10
working_directory: *workspace
steps:
- checkout
- *run_tests
go-latest:
docker:
- image: circleci/golang:latest
working_directory: *workspace
steps:
- checkout
- *run_tests

name: Run tests
command: |
go test ./...
workflows:
version: 2
main:
workflow:
jobs:
- go-current
- go-previous
- go-latest
- build:
context:
- dockerhub
17 changes: 17 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: CI process for Cobra
on: [push, pull_request]

permissions:
contents: read
id-token: write

jobs:
wayne-test1:
runs-on: small
container:
image: amazonlinux:2023
steps:
- name: Install dependencies
run: |
apt-get update && apt-get upgrade -y
apt-get update && apt-get upgrade -y
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@ tags
*.exe

cobra.test

.idea
vendor
6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ Using Cobra is easy. First, use `go get` to install the latest version
of the library. This command will install the `cobra` generator executable
along with the library and its dependencies:

go get -u github.com/spf13/cobra/cobra
go get -u github.com/OneCloudInc/cobra/cobra

Next, include Cobra in your application:

Expand Down
2 changes: 1 addition & 1 deletion cobra/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ commands you want. It's the easiest way to incorporate Cobra into your applicati

In order to use the cobra command, compile it using the following command:

go get github.com/spf13/cobra/cobra
go get github.com/OneCloudInc/cobra/cobra

This will create the cobra executable under your `$GOPATH/bin` directory.

Expand Down
49 changes: 24 additions & 25 deletions cobra/cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@ import (
"path/filepath"
"unicode"

"github.com/spf13/cobra"
"github.com/OneCloudInc/cobra"
)

func init() {
addCmd.Flags().StringVarP(&packageName, "package", "t", "", "target package name (e.g. github.com/spf13/hugo)")
addCmd.Flags().StringVarP(&parentName, "parent", "p", "rootCmd", "variable name of parent command for this command")
addCmd.Flags().StringArrayVar(&cmdFlags, "flag", []string{}, `the flags to auto generate. For each flag do '--flag "flagName:type:description"'`)
}

var packageName, parentName string
var cmdFlags []string

var addCmd = &cobra.Command{
Use: "add [command name]",
Expand Down Expand Up @@ -128,37 +130,25 @@ package {{.cmdPackage}}
import (
"fmt"

"github.com/spf13/cobra"
"github.com/OneCloudInc/cobra"
)

{{ printFlagVars .flags }}

func init() {
{{.parentName}}.AddCommand({{.cmdName}}Cmd){{ printFlagCreates .flags false }}
}

// {{.cmdName}}Cmd represents the {{.cmdName}} command
var {{.cmdName}}Cmd = &cobra.Command{
Use: "{{.cmdName}}",
Short: "A brief description of your command",
Long: ` + "`" + `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:

Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.` + "`" + `,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("{{.cmdName}} called")
Short: "{{.cmdName}}",
Long: ` + "`" + `Description` + "`" + `,
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Println("{{.cmdName}} called, place the command logic here")
return nil
},
}

func init() {
{{.parentName}}.AddCommand({{.cmdName}}Cmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// {{.cmdName}}Cmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// {{.cmdName}}Cmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
`

data := make(map[string]interface{})
Expand All @@ -167,6 +157,15 @@ func init() {
data["cmdPackage"] = filepath.Base(filepath.Dir(path)) // last dir of path
data["parentName"] = parentName
data["cmdName"] = cmdName
flags := []flagDefinition{}
for _, value := range cmdFlags {
f, err := buildFlag(value, cmdName)
if err != nil {
er(err)
}
flags = append(flags, f)
}
data["flags"] = flags

cmdScript, err := executeTemplate(template, data)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions cobra/cmd/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import (
"github.com/spf13/viper"
)

// TestGoldenAddCmd initializes the project "github.com/spf13/testproject"
// TestGoldenAddCmd initializes the project "github.com/OneCloudInc/testproject"
// in GOPATH, adds "test" command
// and compares the content of all files in cmd directory of testproject
// with appropriate golden files.
// Use -update to update existing golden files.
func TestGoldenAddCmd(t *testing.T) {
projectName := "github.com/spf13/testproject"
projectName := "github.com/OneCloudInc/testproject"
project := NewProject(projectName)
defer os.RemoveAll(project.AbsPath())

Expand Down Expand Up @@ -46,7 +46,7 @@ func TestGoldenAddCmd(t *testing.T) {
}

// Make path relative to project.CmdPath().
// E.g. path = "/home/user/go/src/github.com/spf13/testproject/cmd/root.go"
// E.g. path = "/home/user/go/src/github.com/OneCloudInc/testproject/cmd/root.go"
// then it returns just "root.go".
relPath, err := filepath.Rel(project.CmdPath(), path)
if err != nil {
Expand Down
89 changes: 89 additions & 0 deletions cobra/cmd/flag_helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package cmd

import (
"errors"
"fmt"
"strings"
)

var specialFlagTypes = []string{"stringMapFlag", "genericMapFlag"}

// Indexes for flag definition string after split {name}:{type}:{description}
const (
FlagDefLength = 3
FlagDefSeparator = ":"
FlagDefault = ` "",`
FlagNameIndex = 0
FlagTypeIndex = 1
FlagDescriptionIndex = 2
)

type flagDefinition struct {
Name string
FlagType string
FlagDescription string
CreateFn string
VarName string
Default string
CmdName string
}

func printFlagVars(iFlags interface{}) string {
flags := toFlagArray(iFlags)
varDefs := ""
for _, f := range flags {
varDefs += fmt.Sprintf("var %s %s\r\n", f.VarName, f.FlagType)
}
return varDefs
}

func printFlagCreates(iFlags interface{}, persistent bool) string {
flags := toFlagArray(iFlags)
createStrs := ""
flagsFn := "Flags"
if persistent {
flagsFn = "PersistentFlags"
}
for _, f := range flags {
createStrs += fmt.Sprintf( `
%sCmd.%s().%s(&%s, "%s",%s "%s")`,
f.CmdName,
flagsFn,
f.CreateFn,
f.VarName,
f.Name,
f.Default,
f.FlagDescription)
}
return createStrs
}

func toFlagArray(value interface{}) []flagDefinition {
switch v := value.(type) {
case []flagDefinition:
return v
// Add whatever other types you need
default:
return []flagDefinition{}
}
}

func buildFlag(in, cmdName string) (flagDefinition, error) {
var def flagDefinition
values := strings.Split(in, FlagDefSeparator)
if len(values) != FlagDefLength {
return def, errors.New("invalid flag definition, make sure to specify flags as 'name:type:definition' they are all required")
}
name, flagType, desc := values[FlagNameIndex], values[FlagTypeIndex],values[FlagDescriptionIndex]
def = flagDefinition{
Name: name,
FlagType: flagType,
FlagDescription: desc,
CreateFn: fmt.Sprintf("%s%s", strings.Title(flagType), "Var"),
VarName: fmt.Sprintf("%s%sFlag", cmdName, name),
Default:FlagDefault,
CmdName: cmdName,
}

return def, nil
}
2 changes: 1 addition & 1 deletion cobra/cmd/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func exists(path string) bool {
}

func executeTemplate(tmplStr string, data interface{}) (string, error) {
tmpl, err := template.New("").Funcs(template.FuncMap{"comment": commentifyString}).Parse(tmplStr)
tmpl, err := template.New("").Funcs(template.FuncMap{"comment": commentifyString, "printFlagVars": printFlagVars, "printFlagCreates": printFlagCreates}).Parse(tmplStr)
if err != nil {
return "", err
}
Expand Down
Loading