Create, list, destroy repos. Now 100x moar efficient.

This commit is contained in:
Mr. Is 2016-07-27 18:06:32 -04:00
parent 59b874ea7a
commit 7ed05d13dc
7 changed files with 444 additions and 186 deletions

82
cmd/repo-create.go Normal file
View File

@ -0,0 +1,82 @@
// Copyright © 2016 NAME HERE <EMAIL ADDRESS>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd
import (
"fmt"
"github.com/gogits/go-gogs-client"
"github.com/spf13/cobra"
)
// Flags.
var repoName string
var repoDescription string
var repoIsPrivate bool
var orgName string
var createCmd = &cobra.Command{
Use: "create [my-new-repo | [-n | --name]] [-d | --desc] [-org | --org] [-p | --private]]",
Short: "Create a repository",
Long: `Create a repository for you or your organization.`,
Run: func(cmd *cobra.Command, args []string) {
// accept gogs repo create thingeys || gogs repo create -name=thingeys
if repoName == "" {
repoName = args[0]
}
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 init() {
repoCmd.AddCommand(createCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// createCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// createCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
createCmd.Flags().StringVarP(&repoName, "name", "n", "", "repo name")
createCmd.Flags().StringVarP(&repoDescription, "desc", "d", "", "repo description")
createCmd.Flags().BoolVarP(&repoIsPrivate, "private", "p", false, "repo is private")
createCmd.Flags().StringVarP(&orgName, "org", "o", "", "organization")
}

84
cmd/repo-destroy.go Normal file
View File

@ -0,0 +1,84 @@
// Copyright © 2016 NAME HERE <EMAIL ADDRESS>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd
import (
"fmt"
"strings"
"github.com/spf13/cobra"
)
// destroyCmd represents the destroy command
var destroyCmd = &cobra.Command{
Use: "destroy [username/repo-name]",
Short: "Destroy a given repo.",
Long: `Destroy a repo by username/repo-name. CAREFUL! You won't be asked twice.`,
Run: func(cmd *cobra.Command, args []string) {
var owner string
var repo string
// ia/testes || ia testes
if (len(args) == 0) || (len(args) > 2) {
fmt.Println("Please argue me [username/repo-name] or [username repo-name].")
return
}
// ia/testes
if len(args) == 1 {
slasher := strings.Split(args[0], "/")
if len(slasher) == 2 {
owner, repo = slasher[0], slasher[1]
} else {
fmt.Println("Please argue me [username/repo-name] or [username repo-name].")
return
}
}
// ia testes
if len(args) == 2 {
owner, repo = args[0], args[1]
}
if (owner == "") || (repo == "") {
fmt.Println("Please argue me [username/repo-name] or [username repo-name].")
return
}
err := GetClient().DeleteRepo(owner, repo)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("Deleted %v/%v.\n\n", owner, repo)
},
}
func init() {
repoCmd.AddCommand(destroyCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// destroyCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// destroyCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

55
cmd/repo-list.go Normal file
View File

@ -0,0 +1,55 @@
// Copyright © 2016 NAME HERE <EMAIL ADDRESS>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd
import (
"fmt"
"github.com/spf13/cobra"
)
var listCmd = &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)
}
},
}
func init() {
repoCmd.AddCommand(listCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// listCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// listCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
//
// TODO
}

197
cmd/repo.ex.go Normal file
View File

@ -0,0 +1,197 @@
// Copyright © 2016 NAME HERE <EMAIL ADDRESS>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd
func asdf() {}
// import (
// "fmt"
// "github.com/gogits/go-gogs-client"
// "github.com/spf13/cobra"
// )
// // Flags.
// var repoName string
// var repoDescription string
// var repoIsPrivate bool
// var orgName string
// var repoRootCmd = &cobra.Command{
// Use: "repo",
// Short: "Manage your repositories.",
// Long: "Definitely manage your repositories.",
// Run: func(cmd *cobra.Command, args []string) {
// // do nothing?
// // debug...
// client := GetClient()
// fmt.Printf("\nClient: %v\n", client)
// repos, err := client.ListMyRepos()
// if err != nil {
// fmt.Println(err)
// return
// }
// for _, repo := range repos {
// printRepo(repo)
// }
// },
// }
// 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
// // and all subcommands, e.g.:
// // repoCmd.PersistentFlags().String("foo", "", "A help for foo")
// // 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")
// 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))
// }
// }

View File

@ -21,81 +21,19 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
// Flags.
var repoName string
var repoDescription string
var repoIsPrivate bool
var orgName string
var repoRootCmd = &cobra.Command{
Use: "repo",
Short: "Manage your repositories.",
Long: "Definitely manage your repositories.",
Run: func(cmd *cobra.Command, args []string) {
// do nothing?
// debug...
client := GetClient()
fmt.Printf("\nClient: %v\n", client)
repos, err := client.ListMyRepos()
if err != nil {
fmt.Println(err)
return
}
for _, repo := range repos {
printRepo(repo)
}
},
}
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 // repoCmd represents the repo command
var repoCreateCmd = &cobra.Command{ var repoCmd = &cobra.Command{
Use: "create", Use: "repo",
Short: "Create a repository", Short: "A brief description of your command",
Long: `Create a repository for you or your organization.`, Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
// TODO: Work your own magic here
createRepoOpts := gogs.CreateRepoOption{ fmt.Println("repo called")
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)
}, },
} }
@ -104,6 +42,8 @@ func printRepo(repo *gogs.Repository) {
} }
func init() { func init() {
RootCmd.AddCommand(repoCmd)
// Here you will define your flags and configuration settings. // Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command // Cobra supports Persistent Flags which will work for this command
@ -113,83 +53,5 @@ func init() {
// Cobra supports local flags which will only run when this command // Cobra supports local flags which will only run when this command
// is called directly, e.g.: // is called directly, e.g.:
// repoCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") // repoCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
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))
// }
// }

View File

@ -65,45 +65,33 @@ 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() { func init() {
cobra.OnInitialize(initConfig, initClient) 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. // Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags, which, if defined here, // Cobra supports Persistent Flags, which, if defined here,
// will be global for your application. // will be global for your application.
RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.gogs-cli.yaml)") 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", viper.GetString("api_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)") RootCmd.PersistentFlags().StringVar(&tokenArg, "token", viper.GetString("token"), "token authorization (if not specified in cfg file)")
// Cobra also supports local flags, which will only run // Cobra also supports local flags, which will only run
// when this action is called directly. // when this action is called directly.
RootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") RootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
} }
func GetClient() *gogs.Client {
fmt.Println("Getting client....")
return client
}
func initClient() {
url := viper.GetString("api_url")
token := viper.GetString("token")
client = gogs.NewClient(url, token)
}
// initConfig reads in config file and ENV variables if set. // initConfig reads in config file and ENV variables if set.
func initConfig() { func initConfig() {
if cfgFile != "" { // enable ability to specify config file via flag if cfgFile != "" { // enable ability to specify config file via flag
@ -122,14 +110,4 @@ func initConfig() {
fmt.Println("No configuration file found.") fmt.Println("No configuration file found.")
} }
// if api_url was flagged, set it to that (override cfg)
// if apiURL != "" {
// viper.Set("api_url", apiURL)
// }
// // if token was flagged, set it to that (override cfg)
// if tokenArg != "" {
// viper.Set("token", tokenArg)
// }
} }

BIN
gogs

Binary file not shown.