Skip to content
This repository was archived by the owner on Feb 5, 2021. It is now read-only.

Commit cc25629

Browse files
nadundesilvaisurulucky
authored andcommitted
Add -u and -p flags for push and pull commands
1 parent 5a5f5ed commit cc25629

File tree

6 files changed

+98
-54
lines changed

6 files changed

+98
-54
lines changed

components/cli/cmd/cellery/pull.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,17 @@
1919
package main
2020

2121
import (
22+
"fmt"
23+
2224
"github.com/spf13/cobra"
2325

2426
"github.com/cellery-io/sdk/components/cli/pkg/commands"
2527
"github.com/cellery-io/sdk/components/cli/pkg/util"
2628
)
2729

2830
func newPullCommand() *cobra.Command {
31+
var username string
32+
var password string
2933
cmd := &cobra.Command{
3034
Use: "pull [<registry>/]<organization>/<cell-image>:<version>",
3135
Short: "Pull cell image from the remote repository",
@@ -38,13 +42,18 @@ func newPullCommand() *cobra.Command {
3842
if err != nil {
3943
return err
4044
}
45+
if password != "" && username == "" {
46+
return fmt.Errorf("expects username if the password is provided, username not provided")
47+
}
4148
return nil
4249
},
4350
Run: func(cmd *cobra.Command, args []string) {
44-
commands.RunPull(args[0], false)
51+
commands.RunPull(args[0], false, username, password)
4552
},
4653
Example: " cellery pull cellery-samples/employee:1.0.0\n" +
4754
" cellery pull registry.foo.io/cellery-samples/employee:1.0.0",
4855
}
56+
cmd.Flags().StringVarP(&username, "username", "u", "", "Username for Cellery Registry")
57+
cmd.Flags().StringVarP(&password, "password", "p", "", "Password for Cellery Registry")
4958
return cmd
5059
}

components/cli/cmd/cellery/push.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,17 @@
1919
package main
2020

2121
import (
22+
"fmt"
23+
2224
"github.com/spf13/cobra"
2325

2426
"github.com/cellery-io/sdk/components/cli/pkg/commands"
2527
"github.com/cellery-io/sdk/components/cli/pkg/util"
2628
)
2729

2830
func newPushCommand() *cobra.Command {
31+
var username string
32+
var password string
2933
cmd := &cobra.Command{
3034
Use: "push [<registry>/]<organization>/<cell-image>:<version>",
3135
Short: "Push cell image to the remote repository",
@@ -38,13 +42,18 @@ func newPushCommand() *cobra.Command {
3842
if err != nil {
3943
return err
4044
}
45+
if password != "" && username == "" {
46+
return fmt.Errorf("expects username if the password is provided, username not provided")
47+
}
4148
return nil
4249
},
4350
Run: func(cmd *cobra.Command, args []string) {
44-
commands.RunPush(args[0])
51+
commands.RunPush(args[0], username, password)
4552
},
4653
Example: " cellery push cellery-samples/employee:1.0.0\n" +
4754
" cellery push registry.foo.io/cellery-samples/employee:1.0.0",
4855
}
56+
cmd.Flags().StringVarP(&username, "username", "u", "", "Username for Cellery Registry")
57+
cmd.Flags().StringVarP(&password, "password", "p", "", "Password for Cellery Registry")
4958
return cmd
5059
}

components/cli/pkg/commands/build.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ func generateMetaData(cellImage *util.CellImage, targetDir string, spinner *util
223223
dependencyExists, err := util.FileExists(cellImageZip)
224224
if !dependencyExists {
225225
spinner.Pause()
226-
RunPull(dependency, true)
226+
RunPull(dependency, true, "", "")
227227
fmt.Println()
228228
spinner.Resume()
229229
}

components/cli/pkg/commands/pull.go

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -37,31 +37,42 @@ import (
3737

3838
// RunPull connects to the Cellery Registry and pulls the cell image and saves it in the local repository.
3939
// This also adds the relevant ballerina files to the ballerina repo directory.
40-
func RunPull(cellImage string, isSilent bool) {
40+
func RunPull(cellImage string, isSilent bool, username string, password string) {
4141
parsedCellImage, err := util.ParseImageTag(cellImage)
4242
if err != nil {
4343
util.ExitWithErrorMessage("Error occurred while parsing cell image", err)
4444
}
4545

46-
// Instantiating a native keyring
47-
ring, err := keyring.Open(keyring.Config{
48-
ServiceName: constants.CELLERY_HUB_KEYRING_NAME,
49-
})
50-
if err != nil {
51-
util.ExitWithErrorMessage("Error occurred while logging out", err)
46+
var registryCredentials = &util.RegistryCredentials{
47+
Username: username,
48+
Password: password,
5249
}
53-
54-
// Reading the existing credentials
55-
var registryCredentials = &util.RegistryCredentials{}
56-
if err == nil {
57-
ringItem, err := ring.Get(parsedCellImage.Registry)
58-
if err == nil && ringItem.Data != nil {
59-
err = json.Unmarshal(ringItem.Data, registryCredentials)
60-
}
50+
if username != "" && password == "" {
51+
username, password, err = util.RequestCredentials("Cellery Registry", username)
6152
}
6253
isCredentialsPresent := err == nil && registryCredentials.Username != "" &&
6354
registryCredentials.Password != ""
6455

56+
var ring keyring.Keyring
57+
if !isCredentialsPresent {
58+
// Instantiating a native keyring
59+
ring, err := keyring.Open(keyring.Config{
60+
ServiceName: constants.CELLERY_HUB_KEYRING_NAME,
61+
})
62+
if err != nil {
63+
util.ExitWithErrorMessage("Error occurred while logging out", err)
64+
}
65+
66+
if err == nil {
67+
ringItem, err := ring.Get(parsedCellImage.Registry)
68+
if err == nil && ringItem.Data != nil {
69+
err = json.Unmarshal(ringItem.Data, registryCredentials)
70+
}
71+
}
72+
isCredentialsPresent = err == nil && registryCredentials.Username != "" &&
73+
registryCredentials.Password != ""
74+
}
75+
6576
if isCredentialsPresent {
6677
// Pulling the image using the saved credentials
6778
err = pullImage(parsedCellImage, registryCredentials.Username, registryCredentials.Password)
@@ -92,16 +103,18 @@ func RunPull(cellImage string, isSilent bool) {
92103
if err != nil {
93104
util.ExitWithErrorMessage("Error occurred while saving Credentials", err)
94105
}
95-
err = ring.Set(keyring.Item{
96-
Key: parsedCellImage.Registry,
97-
Data: credentialsData,
98-
})
99-
if err == nil {
100-
fmt.Printf("\n%s Saved Credentials for %s Registry", util.GreenBold("\U00002714"),
101-
util.Bold(parsedCellImage.Registry))
102-
} else {
103-
fmt.Printf("\n\n%s %s", util.YellowBold("\U000026A0"),
104-
"Error occurred while saving credentials")
106+
if ring != nil {
107+
err = ring.Set(keyring.Item{
108+
Key: parsedCellImage.Registry,
109+
Data: credentialsData,
110+
})
111+
if err == nil {
112+
fmt.Printf("\n%s Saved Credentials for %s Registry", util.GreenBold("\U00002714"),
113+
util.Bold(parsedCellImage.Registry))
114+
} else {
115+
fmt.Printf("\n\n%s %s", util.YellowBold("\U000026A0"),
116+
"Error occurred while saving credentials")
117+
}
105118
}
106119
} else {
107120
util.ExitWithErrorMessage("Failed to pull image", err)

components/cli/pkg/commands/push.go

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -44,31 +44,42 @@ import (
4444

4545
// RunPush parses the cell image name to recognize the Cellery Registry (A Docker Registry), Organization and version
4646
// and pushes to the Cellery Registry
47-
func RunPush(cellImage string) {
47+
func RunPush(cellImage string, username string, password string) {
4848
parsedCellImage, err := util.ParseImageTag(cellImage)
4949
if err != nil {
5050
util.ExitWithErrorMessage("Error occurred while parsing cell image", err)
5151
}
5252

53-
// Instantiating a native keyring
54-
ring, err := keyring.Open(keyring.Config{
55-
ServiceName: constants.CELLERY_HUB_KEYRING_NAME,
56-
})
57-
if err != nil {
58-
util.ExitWithErrorMessage("Error occurred while logging out", err)
53+
var registryCredentials = &util.RegistryCredentials{
54+
Username: username,
55+
Password: password,
5956
}
60-
61-
// Reading the existing credentials
62-
var registryCredentials = &util.RegistryCredentials{}
63-
if err == nil {
64-
ringItem, err := ring.Get(parsedCellImage.Registry)
65-
if err == nil && ringItem.Data != nil {
66-
err = json.Unmarshal(ringItem.Data, registryCredentials)
67-
}
57+
if username != "" && password == "" {
58+
username, password, err = util.RequestCredentials("Cellery Registry", username)
6859
}
6960
isCredentialsPresent := err == nil && registryCredentials.Username != "" &&
7061
registryCredentials.Password != ""
7162

63+
var ring keyring.Keyring
64+
if !isCredentialsPresent {
65+
// Instantiating a native keyring
66+
ring, err := keyring.Open(keyring.Config{
67+
ServiceName: constants.CELLERY_HUB_KEYRING_NAME,
68+
})
69+
if err != nil {
70+
util.ExitWithErrorMessage("Error occurred while logging out", err)
71+
}
72+
73+
if err == nil {
74+
ringItem, err := ring.Get(parsedCellImage.Registry)
75+
if err == nil && ringItem.Data != nil {
76+
err = json.Unmarshal(ringItem.Data, registryCredentials)
77+
}
78+
}
79+
isCredentialsPresent = err == nil && registryCredentials.Username != "" &&
80+
registryCredentials.Password != ""
81+
}
82+
7283
if isCredentialsPresent {
7384
// Pushing the image using the saved credentials
7485
err = pushImage(parsedCellImage, registryCredentials.Username, registryCredentials.Password)
@@ -99,16 +110,18 @@ func RunPush(cellImage string) {
99110
if err != nil {
100111
util.ExitWithErrorMessage("Error occurred while saving Credentials", err)
101112
}
102-
err = ring.Set(keyring.Item{
103-
Key: parsedCellImage.Registry,
104-
Data: credentialsData,
105-
})
106-
if err == nil {
107-
fmt.Printf("\n%s Saved Credentials for %s Registry", util.GreenBold("\U00002714"),
108-
util.Bold(parsedCellImage.Registry))
109-
} else {
110-
fmt.Printf("\n\n%s %s", util.YellowBold("\U000026A0"),
111-
"Error occurred while saving credentials")
113+
if ring != nil {
114+
err = ring.Set(keyring.Item{
115+
Key: parsedCellImage.Registry,
116+
Data: credentialsData,
117+
})
118+
if err == nil {
119+
fmt.Printf("\n%s Saved Credentials for %s Registry", util.GreenBold("\U00002714"),
120+
util.Bold(parsedCellImage.Registry))
121+
} else {
122+
fmt.Printf("\n\n%s %s", util.YellowBold("\U000026A0"),
123+
"Error occurred while saving credentials")
124+
}
112125
}
113126
} else {
114127
util.ExitWithErrorMessage("Failed to pull image", err)

components/cli/pkg/commands/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,7 @@ func extractImage(cellImage *util.CellImage, spinner *util.Spinner) (string, err
788788
}
789789
if !imageExists {
790790
spinner.Pause()
791-
RunPull(cellImageTag, true)
791+
RunPull(cellImageTag, true, "", "")
792792
fmt.Println()
793793
spinner.Resume()
794794
}

0 commit comments

Comments
 (0)