Refactor: apply new internal structurs #206

Merged
lunny merged 22 commits from 6543/tea:refactor_new-internal-structure into master 2020-09-30 05:11:35 +00:00
5 changed files with 212 additions and 167 deletions
Showing only changes of commit 734909f462 - Show all commits

View File

@ -5,13 +5,6 @@
package cmd
import (
"fmt"
"log"
"code.gitea.io/tea/modules/interact"
"code.gitea.io/tea/modules/intern"
"github.com/skratchdot/open-golang/open"
"github.com/urfave/cli/v2"
)
@ -20,7 +13,7 @@ var CmdLogin = cli.Command{
Name: "login",
Usage: "Log in to a Gitea server",
Description: `Log in to a Gitea server`,
Action: runLoginAddInteractive,
Action: runLoginAddInteractive, // TODO show list if no arg & detail if login as arg
Subcommands: []*cli.Command{
&cmdLoginList,
&cmdLoginAdd,
@ -28,162 +21,3 @@ var CmdLogin = cli.Command{
&cmdLoginSetDefault,
},
}
// cmdLoginEdit represents to login a gitea server.
var cmdLoginEdit = cli.Command{
Name: "edit",
Usage: "Edit Gitea logins",
Description: `Edit Gitea logins`,
Action: runLoginEdit,
Flags: []cli.Flag{&OutputFlag},
}
func runLoginEdit(ctx *cli.Context) error {
return open.Start(intern.GetConfigPath())
}
// cmdLoginSetDefault represents to login a gitea server.
var cmdLoginSetDefault = cli.Command{
Name: "default",
Usage: "Get or Set Default Login",
Description: `Get or Set Default Login`,
ArgsUsage: "<Login>",
Action: runLoginSetDefault,
Flags: []cli.Flag{&OutputFlag},
}
func runLoginSetDefault(ctx *cli.Context) error {
if err := intern.LoadConfig(); err != nil {
return err
}
if ctx.Args().Len() == 0 {
l, err := intern.GetDefaultLogin()
if err != nil {
return err
}
fmt.Printf("Default Login: %s\n", l.Name)
return nil
}
loginExist := false
for i := range intern.Config.Logins {
intern.Config.Logins[i].Default = false
if intern.Config.Logins[i].Name == ctx.Args().First() {
intern.Config.Logins[i].Default = true
loginExist = true
}
}
if !loginExist {
return fmt.Errorf("login '%s' not found", ctx.Args().First())
}
return intern.SaveConfig()
}
// CmdLogin represents to login a gitea server.
var cmdLoginAdd = cli.Command{
Name: "add",
Usage: "Add a Gitea login",
Description: `Add a Gitea login`,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "name",
Aliases: []string{"n"},
Usage: "Login name",
},
&cli.StringFlag{
Name: "url",
Aliases: []string{"u"},
Value: "https://try.gitea.io",
EnvVars: []string{"GITEA_SERVER_URL"},
Usage: "Server URL",
Required: true,
},
&cli.StringFlag{
Name: "token",
Aliases: []string{"t"},
Value: "",
EnvVars: []string{"GITEA_SERVER_TOKEN"},
Usage: "Access token. Can be obtained from Settings > Applications",
},
&cli.StringFlag{
Name: "user",
Value: "",
EnvVars: []string{"GITEA_SERVER_USER"},
Usage: "User for basic auth (will create token)",
},
&cli.StringFlag{
Name: "password",
Aliases: []string{"pwd"},
Value: "",
EnvVars: []string{"GITEA_SERVER_PASSWORD"},
Usage: "Password for basic auth (will create token)",
},
&cli.StringFlag{
Name: "ssh-key",
Aliases: []string{"s"},
Usage: "Path to a SSH key to use for pull/push operations",
},
&cli.BoolFlag{
Name: "insecure",
Aliases: []string{"i"},
Usage: "Disable TLS verification",
},
},
Action: runLoginAdd,
}
func runLoginAdd(ctx *cli.Context) error {
return intern.AddLogin(
ctx.String("name"),
ctx.String("token"),
ctx.String("user"),
ctx.String("password"),
ctx.String("ssh-key"),
ctx.String("url"),
ctx.Bool("insecure"))
}
func runLoginAddInteractive(_ *cli.Context) error {
return interact.CreateLogin()
}
// CmdLogin represents to login a gitea server.
var cmdLoginList = cli.Command{
Name: "ls",
Usage: "List Gitea logins",
Description: `List Gitea logins`,
Action: runLoginList,
Flags: []cli.Flag{&OutputFlag},
}
func runLoginList(ctx *cli.Context) error {
err := intern.LoadConfig()
if err != nil {
log.Fatal(err)
}
headers := []string{
"Name",
"URL",
"SSHHost",
"User",
"Default",
}
var values [][]string
for _, l := range intern.Config.Logins {
values = append(values, []string{
l.Name,
l.URL,
l.GetSSHHost(),
l.User,
fmt.Sprint(l.Default),
})
}
Output(globalOutputValue, headers, values)
return nil
}

81
cmd/login_add.go Normal file
View File

@ -0,0 +1,81 @@
// Copyright 2020 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package cmd
import (
"code.gitea.io/tea/modules/interact"
"code.gitea.io/tea/modules/intern"
"github.com/urfave/cli/v2"
)
// CmdLogin represents to login a gitea server.
var cmdLoginAdd = cli.Command{
Name: "add",
Usage: "Add a Gitea login",
Description: `Add a Gitea login`,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "name",
Aliases: []string{"n"},
Usage: "Login name",
},
&cli.StringFlag{
Name: "url",
Aliases: []string{"u"},
Value: "https://try.gitea.io",
EnvVars: []string{"GITEA_SERVER_URL"},
Usage: "Server URL",
Required: true,
},
&cli.StringFlag{
Name: "token",
Aliases: []string{"t"},
Value: "",
EnvVars: []string{"GITEA_SERVER_TOKEN"},
Usage: "Access token. Can be obtained from Settings > Applications",
},
&cli.StringFlag{
Name: "user",
Value: "",
EnvVars: []string{"GITEA_SERVER_USER"},
Usage: "User for basic auth (will create token)",
},
&cli.StringFlag{
Name: "password",
Aliases: []string{"pwd"},
Value: "",
EnvVars: []string{"GITEA_SERVER_PASSWORD"},
Usage: "Password for basic auth (will create token)",
},
&cli.StringFlag{
Name: "ssh-key",
Aliases: []string{"s"},
Usage: "Path to a SSH key to use for pull/push operations",
},
&cli.BoolFlag{
Name: "insecure",
Aliases: []string{"i"},
Usage: "Disable TLS verification",
},
},
Action: runLoginAdd,
}
func runLoginAdd(ctx *cli.Context) error {
// TODO: if no args -> interactive
return intern.AddLogin(
ctx.String("name"),
ctx.String("token"),
ctx.String("user"),
ctx.String("password"),
ctx.String("ssh-key"),
ctx.String("url"),
ctx.Bool("insecure"))
}
func runLoginAddInteractive(_ *cli.Context) error {
return interact.CreateLogin()
}

51
cmd/login_default.go Normal file
View File

@ -0,0 +1,51 @@
// Copyright 2020 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package cmd
import (
"fmt"
"code.gitea.io/tea/modules/intern"
"github.com/urfave/cli/v2"
)
// cmdLoginSetDefault represents to login a gitea server.
var cmdLoginSetDefault = cli.Command{
Name: "default",
Usage: "Get or Set Default Login",
Description: `Get or Set Default Login`,
ArgsUsage: "<Login>",
Action: runLoginSetDefault,
Flags: []cli.Flag{&OutputFlag},
}
func runLoginSetDefault(ctx *cli.Context) error {
if err := intern.LoadConfig(); err != nil {
return err
}
if ctx.Args().Len() == 0 {
l, err := intern.GetDefaultLogin()
if err != nil {
return err
}
fmt.Printf("Default Login: %s\n", l.Name)
return nil
}
loginExist := false
for i := range intern.Config.Logins {
intern.Config.Logins[i].Default = false
if intern.Config.Logins[i].Name == ctx.Args().First() {
intern.Config.Logins[i].Default = true
loginExist = true
}
}
if !loginExist {
return fmt.Errorf("login '%s' not found", ctx.Args().First())
}
return intern.SaveConfig()
}

25
cmd/login_edit.go Normal file
View File

@ -0,0 +1,25 @@
// Copyright 2018 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package cmd
import (
"code.gitea.io/tea/modules/intern"
"github.com/skratchdot/open-golang/open"
"github.com/urfave/cli/v2"
)
// cmdLoginEdit represents to login a gitea server.
var cmdLoginEdit = cli.Command{
Name: "edit",
Usage: "Edit Gitea logins",
Description: `Edit Gitea logins`,
Action: runLoginEdit,
Flags: []cli.Flag{&OutputFlag},
}
func runLoginEdit(ctx *cli.Context) error {
return open.Start(intern.GetConfigPath())
}

54
cmd/login_list.go Normal file
View File

@ -0,0 +1,54 @@
// Copyright 2020 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package cmd
import (
"fmt"
"log"
"code.gitea.io/tea/modules/intern"
"github.com/urfave/cli/v2"
)
// CmdLogin represents to login a gitea server.
var cmdLoginList = cli.Command{
Name: "ls",
Usage: "List Gitea logins",
Description: `List Gitea logins`,
Action: runLoginList,
Flags: []cli.Flag{&OutputFlag},
}
func runLoginList(ctx *cli.Context) error {
err := intern.LoadConfig()
if err != nil {
log.Fatal(err)
}
headers := []string{
"Name",
"URL",
"SSHHost",
"User",
"Default",
}
var values [][]string
for _, l := range intern.Config.Logins {
values = append(values, []string{
l.Name,
l.URL,
l.GetSSHHost(),
l.User,
fmt.Sprint(l.Default),
})
}
Output(globalOutputValue, headers, values)
return nil
}