Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
R
rekor
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
SafSec
rekor
Commits
4b6effe0
Commit
4b6effe0
authored
4 years ago
by
Dan Lorenc
Browse files
Options
Downloads
Patches
Plain Diff
Add a json output type to the CLI.
parent
78d257d3
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
.github/workflows/main.yml
+1
-1
1 addition, 1 deletion
.github/workflows/main.yml
cmd/cli/app/format/wrap.go
+18
-7
18 additions, 7 deletions
cmd/cli/app/format/wrap.go
cmd/cli/app/root.go
+26
-0
26 additions, 0 deletions
cmd/cli/app/root.go
with
45 additions
and
8 deletions
.github/workflows/main.yml
+
1
−
1
View file @
4b6effe0
...
...
@@ -50,7 +50,7 @@ jobs:
with
:
args
:
./...
-
name
:
Ensure no files were modified as a result of the build
run
:
git update-index --refresh && git diff-index --quiet HEAD --
run
:
git update-index --refresh && git diff-index --quiet HEAD --
|| git diff
e2e
:
# The type of runner that the job will run on
runs-on
:
ubuntu-latest
...
...
This diff is collapsed.
Click to expand it.
cmd/cli/app/format/wrap.go
+
18
−
7
View file @
4b6effe0
...
...
@@ -6,6 +6,7 @@ import (
"log"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
type
cobraCmd
func
(
cmd
*
cobra
.
Command
,
args
[]
string
)
...
...
@@ -20,14 +21,24 @@ func WrapCmd(f formatCmd) cobraCmd {
}
// TODO: add flags to control output formatting (JSON, plaintext, etc.)
if
s
,
ok
:=
obj
.
(
fmt
.
Stringer
);
ok
{
fmt
.
Print
(
s
.
String
())
}
else
{
b
,
err
:=
json
.
Marshal
(
obj
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
format
:=
viper
.
GetString
(
"format"
)
switch
format
{
case
"default"
:
if
s
,
ok
:=
obj
.
(
fmt
.
Stringer
);
ok
{
fmt
.
Print
(
s
.
String
())
}
else
{
fmt
.
Println
(
toJson
(
s
))
}
fmt
.
Println
(
string
(
b
))
case
"json"
:
fmt
.
Println
(
toJson
(
obj
))
}
}
}
func
toJson
(
i
interface
{})
string
{
b
,
err
:=
json
.
Marshal
(
i
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
return
string
(
b
)
}
This diff is collapsed.
Click to expand it.
cmd/cli/app/root.go
+
26
−
0
View file @
4b6effe0
...
...
@@ -55,6 +55,7 @@ func init() {
rootCmd
.
PersistentFlags
()
.
StringVar
(
&
cfgFile
,
"config"
,
""
,
"config file (default is $HOME/.rekor.yaml)"
)
rootCmd
.
PersistentFlags
()
.
Var
(
&
urlFlag
{
url
:
"http://localhost:3000"
},
"rekor_server"
,
"Server address:port"
)
rootCmd
.
PersistentFlags
()
.
Var
(
&
formatFlag
{
format
:
"default"
},
"format"
,
"Command output format"
)
// these are bound here and not in PreRun so that all child commands can use them
if
err
:=
viper
.
BindPFlags
(
rootCmd
.
PersistentFlags
());
err
!=
nil
{
...
...
@@ -130,3 +131,28 @@ func (u *urlFlag) Set(s string) error {
func
(
u
*
urlFlag
)
Type
()
string
{
return
"url"
}
type
formatFlag
struct
{
format
string
}
func
(
f
*
formatFlag
)
String
()
string
{
return
f
.
format
}
func
(
f
*
formatFlag
)
Set
(
s
string
)
error
{
choices
:=
map
[
string
]
struct
{}{
"default"
:
{},
"json"
:
{}}
if
s
==
""
{
f
.
format
=
"default"
return
nil
}
if
_
,
ok
:=
choices
[
s
];
ok
{
f
.
format
=
s
return
nil
}
return
fmt
.
Errorf
(
"invalid flag value: %s, valid values are [default, json]"
,
s
)
}
func
(
u
*
formatFlag
)
Type
()
string
{
return
"format"
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment