-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
229 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,3 +22,6 @@ _testmain.go | |
*.exe | ||
*.test | ||
*.prof | ||
|
||
.idea | ||
*.iml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,27 @@ | ||
# slack-cli | ||
Command line client for slack post | ||
Simple command-line client for slack by golang | ||
|
||
## Features | ||
* Post message only | ||
* Use Incoming Web hook service on slack | ||
|
||
## Install | ||
```shell | ||
$ go get github.com/takecy/slack-cli | ||
``` | ||
|
||
## Usage | ||
```shell | ||
$ slack-cli config <incoming web hook URL> | ||
$ slack-cli post <message> | ||
``` | ||
post to specific channel (not prefix [#]) | ||
```shell | ||
$ slack-cli post -c general <message> | ||
``` | ||
|
||
## Remove | ||
```shell | ||
$ rm $GOPATH/bin/slack-cli | ||
$ rm -r $HOME/.slack_cli | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/takecy/slack-cli/slacker" | ||
) | ||
|
||
const usage = ` | ||
Usage: | ||
$ slack-cli config <incoming web hook URL> | ||
$ slack-cli post [-c <other channel>] <message> | ||
Commands: | ||
config Set configuration | ||
post Post massage to slack | ||
` | ||
|
||
func main() { | ||
flag.Usage = func() { usageAndExit() } | ||
flag.Parse() | ||
|
||
if flag.NArg() == 0 { | ||
flag.Usage() | ||
return | ||
} | ||
|
||
if flag.Args()[0] == "config" { | ||
if len(flag.Args()) < 2 { | ||
fmt.Fprint(os.Stderr, "Error: URL required. \n") | ||
flag.Usage() | ||
return | ||
} | ||
|
||
slacker.CreateConfig() | ||
c := &slacker.Config{ | ||
URL: flag.Args()[1], | ||
} | ||
slacker.WriteConfig(c) | ||
fmt.Fprint(os.Stdout, "Success: register config \n") | ||
return | ||
} | ||
|
||
if flag.Args()[0] == "post" { | ||
if flag.NArg() < 2 { | ||
flag.Usage() | ||
return | ||
} | ||
|
||
c, err := slacker.ReadConfig() | ||
if err != nil { | ||
fmt.Fprint(os.Stderr, "Error: Failed read config. \n Require [config] command first. \n") | ||
flag.Usage() | ||
return | ||
} | ||
|
||
fs := flag.NewFlagSet(flag.Args()[0], flag.ExitOnError) | ||
ch := fs.String("c", "", "Other Channel") | ||
fs.Parse(flag.Args()[1:]) | ||
|
||
slack := &slacker.Slack{C: c} | ||
err = slack.Post(slacker.Message{ | ||
Text: flag.Args()[flag.NArg()-1], | ||
Channel: "#" + *ch, | ||
}) | ||
|
||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "Error: Failed Post. \n %+v \n", err) | ||
return | ||
} | ||
return | ||
} | ||
|
||
fmt.Fprint(os.Stderr, "Error: Bad command \n") | ||
flag.Usage() | ||
} | ||
|
||
func usageAndExit() { | ||
fmt.Fprintf(os.Stdout, "%s \n", usage) | ||
os.Exit(1) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package slacker | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
) | ||
|
||
// Config is config for slack | ||
type Config struct { | ||
// URL is incoming url by slack | ||
URL string `json:"url"` | ||
} | ||
|
||
var ( | ||
h = os.Getenv("HOME") | ||
confDirName = ".slack_cli" | ||
confFileName = "conf.json" | ||
confDir = fmt.Sprintf("%s/%s", h, confDirName) | ||
confPath = fmt.Sprintf("%s/%s", confDir, confFileName) | ||
) | ||
|
||
// ExistConfig is check conf finle exists | ||
func ExistConfig() bool { | ||
_, err := os.Stat(confPath) | ||
return err == nil | ||
} | ||
|
||
// CreateConfig is initialize conf file | ||
func CreateConfig() (err error) { | ||
err = os.MkdirAll(confDir, 0755) | ||
if err != nil { | ||
return | ||
} | ||
|
||
_, err = os.Create(confPath) | ||
return | ||
} | ||
|
||
// ReadConfig is read conf file | ||
func ReadConfig() (c *Config, err error) { | ||
b, err := ioutil.ReadFile(confPath) | ||
if err != nil { | ||
return | ||
} | ||
|
||
c = new(Config) | ||
err = json.Unmarshal(b, c) | ||
return | ||
} | ||
|
||
// WriteConfig is write to conf file | ||
func WriteConfig(c *Config) (err error) { | ||
b, err := json.Marshal(c) | ||
if err != nil { | ||
return | ||
} | ||
|
||
err = ioutil.WriteFile(confPath, b, 0755) | ||
return | ||
} | ||
|
||
// RemoveConfig is remove conf file | ||
func RemoveConfig() (err error) { | ||
err = os.RemoveAll(confPath) | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package slacker | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"errors" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
// Slack is struct | ||
type Slack struct { | ||
C *Config | ||
} | ||
|
||
// Message is post payload | ||
type Message struct { | ||
Text string `json:"text"` | ||
Channel string `json:"channel"` | ||
} | ||
|
||
var cli = &http.Client{Timeout: time.Duration(5) * time.Second} | ||
|
||
// Post is post message to slack | ||
func (s *Slack) Post(msg Message) (err error) { | ||
b, err := json.Marshal(msg) | ||
if err != nil { | ||
return | ||
} | ||
|
||
buf := bytes.NewBuffer(b) | ||
|
||
url := s.C.URL | ||
req, err := http.NewRequest("POST", url, buf) | ||
if err != nil { | ||
return | ||
} | ||
|
||
res, err := cli.Do(req) | ||
if err != nil { | ||
return | ||
} | ||
|
||
if res.StatusCode != 200 { | ||
return errors.New("post.Failed") | ||
} | ||
|
||
return | ||
} |