Skip to content

Commit e3447a4

Browse files
committed
WIP
1 parent a758776 commit e3447a4

File tree

1 file changed

+29
-30
lines changed

1 file changed

+29
-30
lines changed

main.go

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ const (
106106

107107
const defaultDirMode = os.FileMode(0775) // subject to umask
108108

109-
// FIXME: should this carry SSH keys? if so, sub-structs?
110109
type credential struct {
111110
URL string `json:"url"`
112111
Username string `json:"username"`
@@ -832,21 +831,27 @@ func main() {
832831
}
833832
}
834833

835-
if *flPassword != "" && *flPasswordFile != "" {
836-
handleConfigError(log, true, "ERROR: only one of --password and --password-file may be specified")
837-
}
838834
if *flUsername != "" {
839835
if *flPassword == "" && *flPasswordFile == "" {
840-
handleConfigError(log, true, "ERROR: --password or --password-file must be set when --username is specified")
836+
handleConfigError(log, true, "ERROR: --password or --password-file must be specified when --username is specified")
837+
}
838+
if *flPassword != "" && *flPasswordFile != "" {
839+
handleConfigError(log, true, "ERROR: only one of --password and --password-file may be specified")
840+
}
841+
} else {
842+
if *flPassword != "" {
843+
handleConfigError(log, true, "ERROR: --password may only be specified when --username is specified")
844+
}
845+
if *flPasswordFile != "" {
846+
handleConfigError(log, true, "ERROR: --password-file may only be specified when --username is specified")
841847
}
842848
}
843849
//FIXME: mutex wih flCredentials?
844850

845-
credentials := []credential{}
846851
if len(*flCredentials) > 0 {
847852
for _, cred := range *flCredentials {
848853
if cred.URL == "" {
849-
//FIXME: can it default to --repo?
854+
//FIXME: can it default to --repo? Then --username can be deprecated
850855
handleConfigError(log, true, "ERROR: --credential URL must be specified")
851856
}
852857
if cred.Username == "" {
@@ -858,11 +863,10 @@ func main() {
858863
if cred.Password != "" && cred.PasswordFile != "" {
859864
handleConfigError(log, true, "ERROR: only one of --credential password and password-file may be specified")
860865
}
861-
//FIXME: askpass for this purpose, too?
862-
credentials = append(credentials, cred)
863866
}
864867
}
865868

869+
//FIXME: do I need --ssh at all? With submodules, all sorts of variations can be in flight
866870
if *flSSH {
867871
if *flUsername != "" {
868872
handleConfigError(log, true, "ERROR: only one of --ssh and --username may be specified")
@@ -873,7 +877,7 @@ func main() {
873877
if *flPasswordFile != "" {
874878
handleConfigError(log, true, "ERROR: only one of --ssh and --password-file may be specified")
875879
}
876-
//FIXME: mutex wih flCredentials?
880+
//FIXME: can I use askpass and --credential together?
877881
if *flAskPassURL != "" {
878882
handleConfigError(log, true, "ERROR: only one of --ssh and --askpass-url may be specified")
879883
}
@@ -946,6 +950,17 @@ func main() {
946950
absLink := makeAbsPath(*flLink, absRoot)
947951
absTouchFile := makeAbsPath(*flTouchFile, absRoot)
948952

953+
// Merge credential sources.
954+
if *flUsername != "" {
955+
cred := credential{
956+
URL: *flRepo,
957+
Username: *flUsername,
958+
Password: *flPassword,
959+
PasswordFile: *flPasswordFile,
960+
}
961+
*flCredentials = append([]credential{cred}, (*flCredentials)...)
962+
}
963+
949964
if *flAddUser {
950965
if err := addUser(); err != nil {
951966
log.Error(err, "ERROR: can't add user")
@@ -992,19 +1007,9 @@ func main() {
9921007
os.Exit(1)
9931008
}
9941009

995-
// FIXME: merge into flCredentials
996-
if *flUsername != "" {
997-
if *flPasswordFile != "" {
998-
passwordFileBytes, err := os.ReadFile(*flPasswordFile)
999-
if err != nil {
1000-
log.Error(err, "can't read password file", "file", *flPasswordFile)
1001-
os.Exit(1)
1002-
}
1003-
*flPassword = string(passwordFileBytes)
1004-
}
1005-
}
1006-
//FIXME: merge
1007-
for _, cred := range credentials {
1010+
// Finish populating credentials.
1011+
for i := range *flCredentials {
1012+
cred := &(*flCredentials)[i]
10081013
if cred.PasswordFile != "" {
10091014
passwordFileBytes, err := os.ReadFile(cred.PasswordFile)
10101015
if err != nil {
@@ -1135,14 +1140,8 @@ func main() {
11351140

11361141
// Craft a function that can be called to refresh credentials when needed.
11371142
refreshCreds := func(ctx context.Context) error {
1138-
//FIXME: still mutually exclusive?
11391143
// These should all be mutually-exclusive configs.
1140-
if *flUsername != "" {
1141-
if err := git.StoreCredentials(ctx, git.repo, *flUsername, *flPassword); err != nil {
1142-
return err
1143-
}
1144-
}
1145-
for _, cred := range credentials {
1144+
for _, cred := range *flCredentials {
11461145
if err := git.StoreCredentials(ctx, cred.URL, cred.Username, cred.Password); err != nil {
11471146
return err
11481147
}

0 commit comments

Comments
 (0)