Make vars syntax jive with typical ENV var syntax.

This commit is contained in:
Mr. Is 2016-07-28 10:03:11 -04:00
parent c17f4b38b5
commit 8de0c8c197
6 changed files with 41 additions and 15 deletions

View File

@ -1,5 +1,10 @@
# This file will by default live in $HOME/.go-gogs-cli.yaml. # This file will by default live in $HOME/.go-gogs-cli.yaml.
# You can put it anywhere else, too, but you'll need to tell go-gogs-cli # You can put it anywhere else, too, but you'll need to tell go-gogs-cli
# about it in cmd/root.go (down in initConfig). # about it in cmd/root.go (down in initConfig).
token: 0e6709o05da4753dddf5f592074fdc263f02n801 #
api_url: http://my.goggers.me # Or, you can not use this file and instead set these as ENV variables, like
# export GOGS_TOKEN=qorudfjasij3290r23rkansfj23asdf
# export GOGS_URL=http://my.goggers.me
#
GOGS_TOKEN: 0e6709o05da4753dddf5f592074fdc263f02n801
GOGS_URL: http://my.goggers.me

View File

@ -5,7 +5,7 @@ Accesses the [Gogs Client API](https://github.com/gogits/go-gogs-client), which
## Setup. ## Setup.
Clone the repo and build it yourself, or `go get github.com/irstacks/go-gogs-cli`. Just make sure the `gogs` executable winds up somewhere in your `$PATH`. Clone the repo and build and install it yourself, or `go get github.com/irstacks/go-gogs-cli`. Just make sure the `gogs` executable winds up somewhere in your `$PATH`.
Oh, _but how do you build it?_, you ask? _What path?_ you ask? Oh, _but how do you build it?_, you ask? _What path?_ you ask?
@ -26,14 +26,14 @@ $ git clone https://github.com/irstacks/go-gogs-cli.git
$ go get stuff $ go get stuff
$ go get morestuff $ go get morestuff
``` ```
Now, I say almost-as-marvelous because if you use `git clone` you may run into issues about your $GOPATH. It happens. Since I haven't figured out how to consistently `go get` all the dependencies I need for a given Go project from esoteric locations outside my $GOPATH (in part because I have so many esoteric locations and don't want to haggle with forever adjusting/amending my $GOPATH extensioners), I usually just wind up running `go run main.go` or `go build -o gogs` and `go get`ting the dependencies it complains about one-by-one. I know, it sucks. But that's just how I roll sometimes. I say almost-as-marvelous because if you use `git clone` you may run into issues about your $GOPATH. It happens. Since I haven't figured out how to consistently `go get` all the dependencies I need for a given Go project from esoteric locations outside my $GOPATH (in part because I have so many esoteric locations and don't want to haggle with forever adjusting/amending my $GOPATH extensioners), I usually just wind up running `go run main.go` or `go build -o gogs` and `go get`ting the dependencies it complains about one-by-one. I know, it sucks. But that's just how I roll sometimes.
<br> <br>
<br> <br>
Finally, we can build the sucker. Finally, we can build the sucker (this is __optional__ -- the repo comes with a build `gogs`).
```bash ```bash
$ go build -o gogs $ go build -o gogs
``` ```
Here we're using -o to tell go where to build the build it makes, in this case a file in the same directory we're with the name 'gogs' (because thats a lot shorter than go-gogs-cli). Note that whatever you name this file it what it will be accessible for you as on the CLI. So if you name it 'goo' (awesome.), then your commands will be all like: `$ goo repo new ...` Here we're using -o to tell go where to build the build it makes, in this case a file in the same directory we're with the name `gogs` (because thats a lot shorter than 'go-gogs-cli'). Note that whatever you name this file is what it will be accessible for you as on the CLI. So if you name it 'goo' (awesome.), then your commands will be all like: `$ goo repo new ...`
Now here you've got some options (probably) about in which of your $PATH's paths you want to stick it. I like to keep my custom thingeys out of the dredges, so I stick mine in `$HOME/bin/` Now here you've got some options (probably) about in which of your $PATH's paths you want to stick it. I like to keep my custom thingeys out of the dredges, so I stick mine in `$HOME/bin/`
<br> <br>
@ -44,7 +44,28 @@ $ cp gogs ~/bin/
<br> <br>
#### This repo's build is for darwin (Mac). #### This repo's build is for darwin (Mac).
You can build for linux with the nifty `env GOOS=linux go build -o gogs`. You can probably build for Windows too but I don't trouble myself with such things. If you're on a Mac and want to build it for your server or something, you can build for linux with the nifty `env GOOS=linux go build -o gogs`. You can probably build for Windows too but I don't trouble myself with such things.
## Config.
Once you've got the project, you'll need to configure your own `GOGS_TOKEN` and `GOGS_URL` variables.
<br>
You can use
```bash
$ go install # From inside the base of the project.
```
which'll create a file called `.go-gogs-cli.yaml` that likes to live at `$HOME/.go-gogs-cli.yaml`. It handles configuring your __Gogs url__ and __token__, like such:
```yaml
GOGS_TOKEN: 0e6709o05da4753dddf5f592374fdc263f02n801
GOGS_URL: http://my.goggers.me
```
Fill that in for your own self.
<br>
<br>
__Or__, if you'd rather use environment variables, that'll work fine too.
```bash
export GOGS_TOKEN=asdlfj239r029fzsfasf923r23f
export GOGS_URL=http://my.goggers.me
```
## Usage. ## Usage.
So far, you can do things. What's that? You can _do things_? Yep! Do things! So far, you can do things. What's that? You can _do things_? Yep! Do things!

View File

@ -30,7 +30,7 @@ func searchRepos(uid, query, limit string) ([]*gogs.Repository, error) {
client := &http.Client{} client := &http.Client{}
path := "/api/v1/repos/search?q=" + query + "&uid=" + uid + "&limit=" + limit path := "/api/v1/repos/search?q=" + query + "&uid=" + uid + "&limit=" + limit
repos, err := getParsedResponse(client, "GET", viper.GetString("api_url")+path, nil, nil) repos, err := getParsedResponse(client, "GET", viper.GetString("GOGS_URL")+path, nil, nil)
return repos, err return repos, err
} }

View File

@ -38,14 +38,14 @@ There's an example .go-gogs-cli.yaml`,
// Uncomment the following line if your bare application // Uncomment the following line if your bare application
// has an action associated with it: // has an action associated with it:
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
token := viper.GetString("token") token := viper.GetString("GOGS_TOKEN")
if token != "" { if token != "" {
fmt.Println("Token authentication enabled.") fmt.Println("Token authentication enabled.")
} else { } else {
fmt.Println("No token found.") fmt.Println("No token found.")
} }
url := viper.GetString("api_url") url := viper.GetString("GOGS_URL")
if url != "" { if url != "" {
fmt.Println("Using API url @ ", url) fmt.Println("Using API url @ ", url)
} else { } else {
@ -71,8 +71,8 @@ func init() {
// 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", viper.GetString("api_url"), "api url should include /api/v1 path (default is try.gogs.io/api/v1)") RootCmd.PersistentFlags().StringVar(&apiURL, "url", viper.GetString("GOGS_URL"), "api url should include /api/v1 path (default is try.gogs.io/api/v1)")
RootCmd.PersistentFlags().StringVar(&tokenArg, "token", viper.GetString("token"), "token authorization (if not specified in cfg file)") RootCmd.PersistentFlags().StringVar(&tokenArg, "token", viper.GetString("GOGS_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.
@ -85,8 +85,8 @@ func GetClient() *gogs.Client {
} }
func initClient() { func initClient() {
url := viper.GetString("api_url") url := viper.GetString("GOGS_URL")
token := viper.GetString("token") token := viper.GetString("GOGS_TOKEN")
client = gogs.NewClient(url, token) client = gogs.NewClient(url, token)
} }

View File

@ -28,7 +28,7 @@ func getResponse(client *http.Client, method, url string, header http.Header, bo
if err != nil { if err != nil {
return nil, err return nil, err
} }
req.Header.Set("Authorization", "token "+viper.GetString("token")) req.Header.Set("Authorization", "token "+viper.GetString("GOGS_TOKEN"))
for k, v := range header { for k, v := range header {
req.Header[k] = v req.Header[k] = v
} }

BIN
gogs

Binary file not shown.