Use go-gogs-client as a dependency. Repos.
This commit is contained in:
239
cmd/repo.go
239
cmd/repo.go
@ -15,111 +15,95 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
|
||||
"github.com/gogits/go-gogs-client"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var newRepo bool
|
||||
var newOrgRepo bool
|
||||
// Flags.
|
||||
var repoName string
|
||||
var repoDescription string
|
||||
var repoIsPrivate bool
|
||||
var orgName string
|
||||
|
||||
var path string
|
||||
|
||||
type repository struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
// repoCmd represents the repo command
|
||||
var repoCmd = &cobra.Command{
|
||||
var repoRootCmd = &cobra.Command{
|
||||
Use: "repo",
|
||||
Short: "Manage gogs repositories",
|
||||
Long: `Supported operations are .`,
|
||||
Short: "Manage your repositories.",
|
||||
Long: "Definitely manage your repositories.",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// do nothing?
|
||||
|
||||
fmt.Println("Args: ", args)
|
||||
// debug...
|
||||
client := GetClient()
|
||||
fmt.Printf("\nClient: %v\n", client)
|
||||
|
||||
if newRepo {
|
||||
// create new repo
|
||||
if len(args) == 0 { // testes || RotBlauer testes
|
||||
// a name for the repo is required
|
||||
fmt.Println("Please provide a name for your new repository.")
|
||||
return
|
||||
repos, err := client.ListMyRepos()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if !newOrgRepo {
|
||||
// create repo for user
|
||||
path = "/user/repos"
|
||||
repoName = args[0]
|
||||
} else {
|
||||
// create repo for organization
|
||||
if len(args) < 2 {
|
||||
fmt.Println("Usage: [orgname] [reponame]")
|
||||
return
|
||||
}
|
||||
path = "/org/" + args[0] + "/repos"
|
||||
repoName = args[1]
|
||||
}
|
||||
|
||||
repo := repository{
|
||||
Name: repoName,
|
||||
}
|
||||
client := &http.Client{}
|
||||
|
||||
jsonString, _ := json.Marshal(repo)
|
||||
|
||||
req, _ := http.NewRequest("POST", viper.GetString("api_url")+path, bytes.NewBuffer(jsonString))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Authorization", ("token " + viper.GetString("token")))
|
||||
|
||||
res, err := client.Do(req)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
defer res.Body.Close()
|
||||
|
||||
body, err := ioutil.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
} else {
|
||||
fmt.Println(string(body))
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
// get all repos owned by authenticated user
|
||||
path = "/user/repos"
|
||||
client := &http.Client{}
|
||||
req, err := http.NewRequest("GET", viper.GetString("api_url")+path, nil)
|
||||
req.Header.Set("Authorization", "token "+viper.GetString("token"))
|
||||
res, err := client.Do(req)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
defer res.Body.Close()
|
||||
|
||||
body, err := ioutil.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
} else {
|
||||
fmt.Println(string(body))
|
||||
}
|
||||
for _, repo := range repos {
|
||||
printRepo(repo)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
RootCmd.AddCommand(repoCmd)
|
||||
var repoListCmnd = &cobra.Command{
|
||||
Use: "list",
|
||||
Short: "List your repositories.",
|
||||
Long: "List all your repositories.",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
repos, err := GetClient().ListMyRepos()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
for _, repo := range repos {
|
||||
printRepo(repo)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
// repoCmd represents the repo command
|
||||
var repoCreateCmd = &cobra.Command{
|
||||
Use: "create",
|
||||
Short: "Create a repository",
|
||||
Long: `Create a repository for you or your organization.`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
|
||||
createRepoOpts := gogs.CreateRepoOption{
|
||||
Name: repoName,
|
||||
Description: repoDescription,
|
||||
Private: repoIsPrivate,
|
||||
}
|
||||
|
||||
var err error
|
||||
var repo *gogs.Repository
|
||||
|
||||
if orgName == "" {
|
||||
repo, err = GetClient().CreateRepo(createRepoOpts)
|
||||
} else {
|
||||
repo, err = GetClient().CreateOrgRepo(orgName, createRepoOpts)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
fmt.Println("Repo created! Woohoo!")
|
||||
printRepo(repo)
|
||||
|
||||
},
|
||||
}
|
||||
|
||||
func printRepo(repo *gogs.Repository) {
|
||||
fmt.Printf("Name: %v\nGit url: %v\n\n", repo.FullName, repo.CloneUrl)
|
||||
}
|
||||
|
||||
func init() {
|
||||
// Here you will define your flags and configuration settings.
|
||||
|
||||
// Cobra supports Persistent Flags which will work for this command
|
||||
@ -129,6 +113,83 @@ func init() {
|
||||
// Cobra supports local flags which will only run when this command
|
||||
// is called directly, e.g.:
|
||||
// repoCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
||||
repoCmd.Flags().BoolVarP(&newRepo, "new", "n", false, "Create a new repository. A name is required.")
|
||||
repoCmd.Flags().BoolVarP(&newOrgRepo, "org", "o", false, "Specify an organization you own.")
|
||||
repoCreateCmd.Flags().StringVarP(&repoName, "name", "n", "", "repo name")
|
||||
repoCreateCmd.Flags().StringVarP(&repoDescription, "desc", "d", "", "repo description")
|
||||
repoCreateCmd.Flags().BoolVarP(&repoIsPrivate, "private", "p", false, "repo is private")
|
||||
repoCreateCmd.Flags().StringVarP(&orgName, "org", "o", "", "organization")
|
||||
|
||||
RootCmd.AddCommand(repoRootCmd)
|
||||
repoRootCmd.AddCommand(repoCreateCmd)
|
||||
}
|
||||
|
||||
// fmt.Println("Args: ", args)
|
||||
|
||||
// if createUserRepoFlag {
|
||||
// // create new repo
|
||||
// if len(args) == 0 { // testes || RotBlauer testes
|
||||
// // a name for the repo is required
|
||||
// fmt.Println("Please provide a name for your new repository.")
|
||||
// return
|
||||
|
||||
// }
|
||||
|
||||
// if !createOrganizationRepoFlag {
|
||||
// // create repo for user
|
||||
// path = "/user/repos"
|
||||
// repoName = args[0]
|
||||
// } else {
|
||||
// // create repo for organization
|
||||
// if len(args) < 2 {
|
||||
// fmt.Println("Usage: [orgname] [reponame]")
|
||||
// return
|
||||
// }
|
||||
// path = "/org/" + args[0] + "/repos"
|
||||
// repoName = args[1]
|
||||
// }
|
||||
|
||||
// repo := repository{
|
||||
// Name: repoName,
|
||||
// }
|
||||
// client := &http.Client{}
|
||||
|
||||
// jsonString, _ := json.Marshal(repo)
|
||||
|
||||
// req, _ := http.NewRequest("POST", viper.GetString("api_url")+path, bytes.NewBuffer(jsonString))
|
||||
// req.Header.Set("Content-Type", "application/json")
|
||||
// req.Header.Set("Authorization", ("token " + viper.GetString("token")))
|
||||
|
||||
// res, err := client.Do(req)
|
||||
// if err != nil {
|
||||
// fmt.Println(err)
|
||||
// }
|
||||
|
||||
// defer res.Body.Close()
|
||||
|
||||
// body, err := ioutil.ReadAll(res.Body)
|
||||
// if err != nil {
|
||||
// fmt.Println(err)
|
||||
// } else {
|
||||
// fmt.Println(string(body))
|
||||
// }
|
||||
|
||||
// } else {
|
||||
|
||||
// // get all repos owned by authenticated user
|
||||
// path = "/user/repos"
|
||||
// client := &http.Client{}
|
||||
// req, err := http.NewRequest("GET", viper.GetString("api_url")+path, nil)
|
||||
// req.Header.Set("Authorization", "token "+viper.GetString("token"))
|
||||
// res, err := client.Do(req)
|
||||
// if err != nil {
|
||||
// fmt.Println(err)
|
||||
// }
|
||||
|
||||
// defer res.Body.Close()
|
||||
|
||||
// body, err := ioutil.ReadAll(res.Body)
|
||||
// if err != nil {
|
||||
// fmt.Println(err)
|
||||
// } else {
|
||||
// fmt.Println(string(body))
|
||||
// }
|
||||
// }
|
||||
|
47
cmd/root.go
47
cmd/root.go
@ -18,12 +18,15 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
gogs "github.com/gogits/go-gogs-client"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var cfgFile string
|
||||
var apiURL string
|
||||
var tokenArg string
|
||||
var client *gogs.Client
|
||||
|
||||
// RootCmd represents the base command when called without any subcommands
|
||||
var RootCmd = &cobra.Command{
|
||||
@ -39,7 +42,7 @@ $HOME/.gogs-cli.yaml. While you're there you can adjust the default to your liki
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
token := viper.GetString("token")
|
||||
if token != "" {
|
||||
fmt.Println("Token authentication enabled.")
|
||||
fmt.Println("Token authentication enabled @ ", token)
|
||||
} else {
|
||||
fmt.Println("No token found.")
|
||||
}
|
||||
@ -62,15 +65,40 @@ func Execute() {
|
||||
}
|
||||
}
|
||||
|
||||
func GetClient() *gogs.Client {
|
||||
fmt.Println("Getting client.")
|
||||
return client
|
||||
}
|
||||
|
||||
func initClient() {
|
||||
fmt.Println("initClient")
|
||||
|
||||
url := viper.GetString("api_url")
|
||||
token := viper.GetString("token")
|
||||
fmt.Printf("api url: %v\n", url)
|
||||
fmt.Printf("token: %v\n", token)
|
||||
|
||||
client = gogs.NewClient(url, token)
|
||||
}
|
||||
|
||||
func init() {
|
||||
cobra.OnInitialize(initConfig)
|
||||
cobra.OnInitialize(initConfig, initClient)
|
||||
|
||||
// url := viper.GetString("api_url")
|
||||
// toke := viper.GetString("token")
|
||||
// fmt.Printf("api url: %v", url)
|
||||
// fmt.Printf("token: %v", toke)
|
||||
|
||||
// client = gogs.NewClient(url, toke)
|
||||
|
||||
// Here you will define your flags and configuration settings.
|
||||
// Cobra supports Persistent Flags, which, if defined here,
|
||||
// will be global for your application.
|
||||
|
||||
RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.gogs-cli.yaml)")
|
||||
RootCmd.PersistentFlags().StringVar(&apiURL, "url", "", "api url should include /api/v1 path (default is try.gogs.io/api/v1)")
|
||||
// RootCmd.PersistentFlags().StringVar(&apiURL, "url", "", "api url should include /api/v1 path (default is try.gogs.io/api/v1)")
|
||||
// RootCmd.PersistentFlags().StringVar(&tokenArg, "token", "", "token authorization (if not specified in cfg file)")
|
||||
|
||||
// Cobra also supports local flags, which will only run
|
||||
// when this action is called directly.
|
||||
RootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
||||
@ -85,7 +113,7 @@ func initConfig() {
|
||||
viper.SetConfigName(".gogs-cli") // name of config file (without extension)
|
||||
viper.AddConfigPath("$HOME") // adding home directory as first search path
|
||||
viper.AutomaticEnv() // read in environment variables that match
|
||||
viper.SetDefault("api_url", "try.gogs.io/api/v1")
|
||||
// viper.SetDefault("api_url", "try.gogs.io/api/v1")
|
||||
|
||||
// If a config file is found, read it in.
|
||||
if err := viper.ReadInConfig(); err == nil {
|
||||
@ -95,8 +123,13 @@ func initConfig() {
|
||||
}
|
||||
|
||||
// if api_url was flagged, set it to that (override cfg)
|
||||
if apiURL != "" {
|
||||
viper.Set("api_url", apiURL)
|
||||
}
|
||||
// if apiURL != "" {
|
||||
// viper.Set("api_url", apiURL)
|
||||
// }
|
||||
|
||||
// // if token was flagged, set it to that (override cfg)
|
||||
// if tokenArg != "" {
|
||||
// viper.Set("token", tokenArg)
|
||||
// }
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user