@@ -3,6 +3,7 @@ package get
33import (
44 "context"
55 "fmt"
6+ "io/ioutil"
67 "os"
78
89 flagutil "github.com/redhat-developer/app-services-cli/pkg/cmdutil/flags"
@@ -20,8 +21,9 @@ import (
2021)
2122
2223type Options struct {
23- artifact string
24- group string
24+ artifact string
25+ group string
26+ outputFile string
2527
2628 version string
2729
@@ -46,16 +48,32 @@ func NewGetCommand(f *factory.Factory) *cobra.Command {
4648 }
4749
4850 cmd := & cobra.Command {
49- Use : "get" ,
50- Short : "Get latest artifact by id and group" ,
51- Long : "" ,
52- Example : "" ,
53- Args : cobra .RangeArgs (0 , 1 ),
51+ Use : "get" ,
52+ Short : "Get latest artifact by id and group" ,
53+ Long : "" ,
54+ Example : `
55+ ## Get latest artifact by name
56+ rhoas service-registry artifacts get myschema
57+
58+ ## Get latest artifact and save its content to file
59+ rhoas service-registry artifacts get myschema myschema.json
60+
61+ ## Get latest artifact and pipe it to other command
62+ rhoas service-registry artifacts get myschema | grep -i 'user'
63+
64+ ## Get latest artifact by specifying custom group, registry and name as flag
65+ rhoas service-registry artifacts get --group mygroup --registryId=myregistry --artifact myartifact.json ",
66+ ` ,
67+ Args : cobra .RangeArgs (0 , 2 ),
5468 RunE : func (cmd * cobra.Command , args []string ) error {
5569 if len (args ) > 0 {
5670 opts .artifact = args [0 ]
5771 }
5872
73+ if len (args ) > 1 {
74+ opts .outputFile = args [1 ]
75+ }
76+
5977 if len (args ) > 0 {
6078 opts .artifact = args [0 ]
6179 }
@@ -81,6 +99,7 @@ func NewGetCommand(f *factory.Factory) *cobra.Command {
8199 cmd .Flags ().StringVarP (& opts .artifact , "artifact" , "a" , "" , "Id of the artifact" )
82100 cmd .Flags ().StringVarP (& opts .group , "group" , "g" , "" , "Id of the artifact" )
83101 cmd .Flags ().StringVarP (& opts .registryID , "registryId" , "" , "" , "Id of the registry to be used. By default uses currently selected registry." )
102+ cmd .Flags ().StringVarP (& opts .outputFile , "outputFile" , "" , "" , "name of the file " )
84103
85104 flagutil .EnableOutputFlagCompletion (cmd )
86105
@@ -116,20 +135,24 @@ func runGet(opts *Options) error {
116135
117136 ctx := context .Background ()
118137 request := dataAPI .ArtifactsApi .GetLatestArtifact (ctx , opts .group , opts .artifact )
119- data , _ , err := request .Execute ()
138+ dataFile , _ , err := request .Execute ()
120139 if err != nil {
121140 return registryinstanceerror .TransformError (err )
122141 }
142+ fileContent , err := ioutil .ReadFile (dataFile .Name ())
143+ if err != nil {
144+ return err
145+ }
146+ if opts .outputFile != "" {
147+ err := os .WriteFile (opts .outputFile , fileContent , 0600 )
148+ if err != nil {
149+ return err
150+ }
151+ } else {
152+ // Print to stdout
153+ fmt .Fprintf (os .Stdout , "%v\n " , string (fileContent ))
154+ }
123155
124- fmt .Fprintf (os .Stdout , "%v\n " , data )
125-
126- // stringContent := new(strings.Builder)
127- // io.Copy(stringContent, data)
128- // if err != nil {
129- // return nil
130- // }
131- // io.WriteString(opts.IO.Out, stringContent.String())
132- logger .Info ("Successfully downloaded artifact" )
133-
156+ logger .Info ("Successfully fetched artifact" )
134157 return nil
135158}
0 commit comments