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
7 changed files with 17 additions and 4 deletions
Showing only changes of commit 84f02526c4 - Show all commits

View File

@ -19,12 +19,13 @@ import (
"gopkg.in/yaml.v2"
)
// Config reprensents local configurations
// LocalConfig represents local configurations
6543 marked this conversation as resolved Outdated
Outdated
Review

LocalConfig

LocalConfig
type LocalConfig struct {
Logins []Login `yaml:"logins"`
}
var (
// Config contain if loaded local tea config
Config LocalConfig
yamlConfigPath string
)
@ -44,10 +45,12 @@ func init() {
yamlConfigPath = filepath.Join(dir, "tea.yml")
}
// GetConfigPath return path to tea config file
func GetConfigPath() string {
return yamlConfigPath
}
// LoadConfig load config into global Config var
func LoadConfig() error {
ymlPath := GetConfigPath()
exist, _ := utils.FileExist(ymlPath)
@ -67,6 +70,7 @@ func LoadConfig() error {
return nil
}
// SaveConfig save config from global Config var into config file
func SaveConfig() error {
ymlPath := GetConfigPath()
bs, err := yaml.Marshal(Config)
@ -141,6 +145,8 @@ func curGitRepoPath(repoValue, remoteValue string) (*Login, string, error) {
return nil, "", errors.New("No Gitea login found. You might want to specify --repo (and --login) to work outside of a repository")
}
// GetOwnerAndRepo return repoOwner and repoName
// based on relative path and default owner (if not in path)
func GetOwnerAndRepo(repoPath, user string) (string, string) {
if len(repoPath) == 0 {
return "", ""

View File

@ -68,6 +68,7 @@ func (l *Login) GetSSHHost() string {
return u.Hostname()
}
// GetDefaultLogin return the default login
func GetDefaultLogin() (*Login, error) {
if len(Config.Logins) == 0 {
return nil, errors.New("No available login")
@ -81,6 +82,7 @@ func GetDefaultLogin() (*Login, error) {
return &Config.Logins[0], nil
}
// GetLoginByName get login by name
func GetLoginByName(name string) *Login {
for _, l := range Config.Logins {
if l.Name == name {
@ -90,6 +92,7 @@ func GetLoginByName(name string) *Login {
return nil
}
// AddLogin add a login to global Config var
func AddLogin(login Login) error {
for _, l := range Config.Logins {
if l.Name == login.Name {

View File

@ -11,6 +11,7 @@ import (
"github.com/charmbracelet/glamour"
)
// IssueDetails print an issue rendered to stdout
func IssueDetails(issue *gitea.Issue) {
in := fmt.Sprintf("# #%d %s (%s)\n%s created %s\n\n%s\n", issue.Index,

View File

@ -10,6 +10,7 @@ import (
"code.gitea.io/sdk/gitea"
)
// MilestoneDetails print an milestone formatted to stdout
func MilestoneDetails(milestone *gitea.Milestone) {
fmt.Printf("%s\n",
milestone.Title,

View File

@ -17,8 +17,8 @@ func getGlamourTheme() string {
return "light"
}
// FormatSize get kb in int and return string
func FormatSize(kb int64) string {
// formatSize get kb in int and return string
func formatSize(kb int64) string {
if kb < 1024 {
return fmt.Sprintf("%d Kb", kb)
}

View File

@ -11,6 +11,7 @@ import (
"github.com/charmbracelet/glamour"
)
// PullDetails print an pull rendered to stdout
func PullDetails(pr *gitea.PullRequest) {
in := fmt.Sprintf("# #%d %s (%s)\n%s created %s\n\n%s\n", pr.Index,

View File

@ -11,6 +11,7 @@ import (
"code.gitea.io/sdk/gitea"
)
// RepoDetails print an repo formatted to stdout
func RepoDetails(repo *gitea.Repository, topics []string) {
output := repo.FullName
if repo.Mirror {
@ -36,7 +37,7 @@ func RepoDetails(repo *gitea.Repository, topics []string) {
repo.OpenIssues,
repo.Stars,
repo.Forks,
FormatSize(int64(repo.Size)),
formatSize(int64(repo.Size)),
)
fmt.Print(output)