-
Notifications
You must be signed in to change notification settings - Fork 2
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
1 parent
0a06742
commit e27d72a
Showing
13 changed files
with
263 additions
and
69 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// Copyright © 2019 NAME HERE <EMAIL ADDRESS> | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package cmd | ||
|
||
import ( | ||
"github.com/ThilinaManamgoda/password-manager/pkg/inputs" | ||
"github.com/ThilinaManamgoda/password-manager/pkg/passwords" | ||
"github.com/pkg/errors" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
// changeCmd represents the change command | ||
var changeCmd = &cobra.Command{ | ||
Use: "change [ID]", | ||
Short: "Change a password entry", | ||
Long: `Change a password entry`, | ||
Args: inputs.HasProvidedValidID(), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
id := args[0] | ||
|
||
mPassword, err := inputs.GetFlagStringVal(cmd, inputs.MasterPassword) | ||
if err != nil { | ||
return errors.Wrapf(err, inputs.ErrMSGCannotGetFlag, mPassword) | ||
} | ||
if mPassword == "" { | ||
mPassword, err = inputs.PromptForMPassword() | ||
if err != nil { | ||
return errors.Wrap(err, "cannot prompt for Master password") | ||
} | ||
} | ||
|
||
passwordRepo, err := passwords.InitPasswordRepo(mPassword) | ||
if err != nil { | ||
return errors.Wrapf(err, "cannot initialize password repository") | ||
} | ||
|
||
passwordEntry, err := passwordRepo.GetPasswordEntry(id) | ||
if err != nil { | ||
return errors.Wrapf(err, "cannot get password entry") | ||
} | ||
|
||
isInteractiveMode, err := inputs.GetFlagBoolVal(cmd, InteractiveMode) | ||
if err != nil { | ||
return err | ||
} | ||
var uN, password string | ||
if isInteractiveMode { | ||
uN, err = inputs.PromptForUsernameWithDefault(passwordEntry.Username) | ||
if err != nil { | ||
return errors.Wrap(err, "cannot prompt for username") | ||
} | ||
password, err = inputs.PromptForUserPasswordWithDefault(passwordEntry.Password) | ||
if err != nil { | ||
return errors.Wrap(err, "cannot prompt for password") | ||
} | ||
} else { | ||
err = inputs.FromFlags(cmd, &uN, &password, nil, nil) | ||
if err != nil { | ||
return errors.Wrapf(err, inputs.ErrMsgCannotGetInput) | ||
} | ||
} | ||
newEntry := passwords.PasswordEntry{ | ||
ID: id, | ||
Username: uN, | ||
Password: password, | ||
} | ||
err = passwordRepo.ChangePasswordEntry(id, newEntry) | ||
if err != nil { | ||
return errors.Wrapf(err, "cannot change password") | ||
} | ||
return nil | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(changeCmd) | ||
|
||
// Here you will define your flags and configuration settings. | ||
|
||
// Cobra supports Persistent Flags which will work for this command | ||
// and all subcommands, e.g.: | ||
// changeCmd.PersistentFlags().String("foo", "", "A help for foo") | ||
|
||
// Cobra supports local flags which will only run when this command | ||
// is called directly, e.g.: | ||
// changeCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") | ||
changeCmd.Flags().StringP(inputs.Password, "p", "", "Password") | ||
changeCmd.Flags().StringP(inputs.Username, "u", "", "User Name") | ||
changeCmd.Flags().BoolP(InteractiveMode, "i", false, "Enable interactive mode") | ||
|
||
} |
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,32 @@ | ||
## password-manager change | ||
|
||
Change a password entry | ||
|
||
### Synopsis | ||
|
||
Change a password entry | ||
|
||
``` | ||
password-manager change [ID] [flags] | ||
``` | ||
|
||
### Options | ||
|
||
``` | ||
-h, --help help for change | ||
-i, --interactive Enable interactive mode | ||
-p, --password string Password | ||
-u, --username string User Name | ||
``` | ||
|
||
### Options inherited from parent commands | ||
|
||
``` | ||
-m, --masterPassword string Master password | ||
``` | ||
|
||
### SEE ALSO | ||
|
||
* [password-manager](password-manager.md) - A local Password Manager | ||
|
||
###### Auto generated by spf13/cobra on 15-Dec-2019 |
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
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
Oops, something went wrong.