-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
General refactor of the cmd package #3328
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
81c27eb
bbb3929
5cd459b
24ccbc1
5ab4396
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
package cmd | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"code.gitea.io/git" | ||
|
@@ -20,9 +21,7 @@ var ( | |
// CmdAdmin represents the available admin sub-command. | ||
CmdAdmin = cli.Command{ | ||
Name: "admin", | ||
Usage: "Perform admin operations on command line", | ||
Description: `Allow using internal logic of Gitea without hacking into the source code | ||
to make automatic initialization process more smoothly`, | ||
Usage: "Command line interface to perform common administrative operations", | ||
Subcommands: []cli.Command{ | ||
subcmdCreateUser, | ||
subcmdChangePassword, | ||
|
@@ -37,17 +36,14 @@ to make automatic initialization process more smoothly`, | |
Flags: []cli.Flag{ | ||
cli.StringFlag{ | ||
Name: "name", | ||
Value: "", | ||
Usage: "Username", | ||
}, | ||
cli.StringFlag{ | ||
Name: "password", | ||
Value: "", | ||
Usage: "User password", | ||
}, | ||
cli.StringFlag{ | ||
Name: "email", | ||
Value: "", | ||
Usage: "User email address", | ||
}, | ||
cli.BoolFlag{ | ||
|
@@ -88,57 +84,43 @@ to make automatic initialization process more smoothly`, | |
) | ||
|
||
func runChangePassword(c *cli.Context) error { | ||
if !c.IsSet("password") { | ||
return fmt.Errorf("Password is not specified") | ||
} else if !c.IsSet("username") { | ||
return fmt.Errorf("Username is not specified") | ||
if err := argsSet(c, "username", "password"); err != nil { | ||
return err | ||
} | ||
|
||
setting.NewContext() | ||
models.LoadConfigs() | ||
|
||
setting.NewXORMLogService(false) | ||
if err := models.SetEngine(); err != nil { | ||
return fmt.Errorf("models.SetEngine: %v", err) | ||
if err := initDB(); err != nil { | ||
return err | ||
} | ||
|
||
uname := c.String("username") | ||
user, err := models.GetUserByName(uname) | ||
if err != nil { | ||
return fmt.Errorf("%v", err) | ||
return err | ||
} | ||
user.Passwd = c.String("password") | ||
if user.Salt, err = models.GetUserSalt(); err != nil { | ||
return fmt.Errorf("%v", err) | ||
return err | ||
} | ||
user.EncodePasswd() | ||
if err := models.UpdateUserCols(user, "passwd", "salt"); err != nil { | ||
return fmt.Errorf("%v", err) | ||
return err | ||
} | ||
|
||
fmt.Printf("User '%s' password has been successfully updated!\n", uname) | ||
fmt.Printf("%s's password has been successfully updated!\n", user.Name) | ||
return nil | ||
} | ||
|
||
func runCreateUser(c *cli.Context) error { | ||
if !c.IsSet("name") { | ||
return fmt.Errorf("Username is not specified") | ||
} else if !c.IsSet("password") { | ||
return fmt.Errorf("Password is not specified") | ||
} else if !c.IsSet("email") { | ||
return fmt.Errorf("Email is not specified") | ||
if err := argsSet(c, "username", "password"); err != nil { | ||
return err | ||
} | ||
|
||
if c.IsSet("config") { | ||
setting.CustomConf = c.String("config") | ||
} | ||
|
||
setting.NewContext() | ||
models.LoadConfigs() | ||
|
||
setting.NewXORMLogService(false) | ||
if err := models.SetEngine(); err != nil { | ||
return fmt.Errorf("models.SetEngine: %v", err) | ||
if err := initDB(); err != nil { | ||
return err | ||
} | ||
|
||
if err := models.CreateUser(&models.User{ | ||
|
@@ -156,13 +138,8 @@ func runCreateUser(c *cli.Context) error { | |
} | ||
|
||
func runRepoSyncReleases(c *cli.Context) error { | ||
|
||
setting.NewContext() | ||
models.LoadConfigs() | ||
|
||
setting.NewXORMLogService(false) | ||
if err := models.SetEngine(); err != nil { | ||
return fmt.Errorf("models.SetEngine: %v", err) | ||
if err := initDB(); err != nil { | ||
return err | ||
} | ||
|
||
log.Trace("Synchronizing repository releases (this may take a while)") | ||
|
@@ -173,8 +150,7 @@ func runRepoSyncReleases(c *cli.Context) error { | |
Private: true, | ||
}) | ||
if err != nil { | ||
log.Fatal(4, "SearchRepositoryByName: %v", err) | ||
return err | ||
return fmt.Errorf("SearchRepositoryByName: %v", err) | ||
} | ||
if len(repos) == 0 { | ||
break | ||
|
@@ -188,11 +164,7 @@ func runRepoSyncReleases(c *cli.Context) error { | |
continue | ||
} | ||
|
||
oldnum, err := models.GetReleaseCountByRepoID(repo.ID, | ||
models.FindReleasesOptions{ | ||
IncludeDrafts: false, | ||
IncludeTags: true, | ||
}) | ||
oldnum, err := getReleaseCount(repo.ID) | ||
if err != nil { | ||
log.Warn(" GetReleaseCountByRepoID: %v", err) | ||
} | ||
|
@@ -203,11 +175,7 @@ func runRepoSyncReleases(c *cli.Context) error { | |
continue | ||
} | ||
|
||
count, err = models.GetReleaseCountByRepoID(repo.ID, | ||
models.FindReleasesOptions{ | ||
IncludeDrafts: false, | ||
IncludeTags: true, | ||
}) | ||
count, err = getReleaseCount(repo.ID) | ||
if err != nil { | ||
log.Warn(" GetReleaseCountByRepoID: %v", err) | ||
continue | ||
|
@@ -220,3 +188,34 @@ func runRepoSyncReleases(c *cli.Context) error { | |
|
||
return nil | ||
} | ||
|
||
func getReleaseCount(id int64) (int64, error) { | ||
return models.GetReleaseCountByRepoID( | ||
id, | ||
models.FindReleasesOptions{ | ||
IncludeTags: true, | ||
}, | ||
) | ||
} | ||
|
||
// argsSet checks that all the required arguments are set. args is a list of | ||
// arguments that must be set in the passed Context. | ||
func argsSet(c *cli.Context, args ...string) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd move these two functions into There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
for _, a := range args { | ||
if !c.IsSet(a) { | ||
return errors.New(a + " is not set") | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func initDB() error { | ||
setting.NewContext() | ||
models.LoadConfigs() | ||
|
||
setting.NewXORMLogService(false) | ||
if err := models.SetEngine(); err != nil { | ||
return fmt.Errorf("models.SetEngine: %v", err) | ||
} | ||
return nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -90,16 +90,16 @@ func pemBlockForKey(priv interface{}) *pem.Block { | |
} | ||
} | ||
|
||
func runCert(ctx *cli.Context) error { | ||
if len(ctx.String("host")) == 0 { | ||
log.Fatal("Missing required --host parameter") | ||
func runCert(c *cli.Context) error { | ||
if err := argsSet(c, "host"); err != nil { | ||
return err | ||
} | ||
|
||
var priv interface{} | ||
var err error | ||
switch ctx.String("ecdsa-curve") { | ||
switch c.String("ecdsa-curve") { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. rename of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would generally agree, however the entire package refers to c *cli.Context instead of ctx *cli.Context. Shall I change everything to use ctx? |
||
case "": | ||
priv, err = rsa.GenerateKey(rand.Reader, ctx.Int("rsa-bits")) | ||
priv, err = rsa.GenerateKey(rand.Reader, c.Int("rsa-bits")) | ||
case "P224": | ||
priv, err = ecdsa.GenerateKey(elliptic.P224(), rand.Reader) | ||
case "P256": | ||
|
@@ -109,23 +109,23 @@ func runCert(ctx *cli.Context) error { | |
case "P521": | ||
priv, err = ecdsa.GenerateKey(elliptic.P521(), rand.Reader) | ||
default: | ||
log.Fatalf("Unrecognized elliptic curve: %q", ctx.String("ecdsa-curve")) | ||
log.Fatalf("Unrecognized elliptic curve: %q", c.String("ecdsa-curve")) | ||
} | ||
if err != nil { | ||
log.Fatalf("Failed to generate private key: %v", err) | ||
} | ||
|
||
var notBefore time.Time | ||
if len(ctx.String("start-date")) == 0 { | ||
notBefore = time.Now() | ||
} else { | ||
notBefore, err = time.Parse("Jan 2 15:04:05 2006", ctx.String("start-date")) | ||
if startDate := c.String("start-date"); startDate != "" { | ||
notBefore, err = time.Parse("Jan 2 15:04:05 2006", startDate) | ||
if err != nil { | ||
log.Fatalf("Failed to parse creation date: %v", err) | ||
} | ||
} else { | ||
notBefore = time.Now() | ||
} | ||
|
||
notAfter := notBefore.Add(ctx.Duration("duration")) | ||
notAfter := notBefore.Add(c.Duration("duration")) | ||
|
||
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128) | ||
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit) | ||
|
@@ -147,7 +147,7 @@ func runCert(ctx *cli.Context) error { | |
BasicConstraintsValid: true, | ||
} | ||
|
||
hosts := strings.Split(ctx.String("host"), ",") | ||
hosts := strings.Split(c.String("host"), ",") | ||
for _, h := range hosts { | ||
if ip := net.ParseIP(h); ip != nil { | ||
template.IPAddresses = append(template.IPAddresses, ip) | ||
|
@@ -156,7 +156,7 @@ func runCert(ctx *cli.Context) error { | |
} | ||
} | ||
|
||
if ctx.Bool("ca") { | ||
if c.Bool("ca") { | ||
template.IsCA = true | ||
template.KeyUsage |= x509.KeyUsageCertSign | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is breaking change and also (name -> username) and also is missing email check
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wooops, looks like I forgot to edit after copypasting - nice catch! (good thing there are code reviews 😌)