Skip to content

Commit

Permalink
動いた
Browse files Browse the repository at this point in the history
  • Loading branch information
ToshihitoKon committed Nov 23, 2023
1 parent a980bd8 commit bb2d2f6
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 74 deletions.
20 changes: 20 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package main

import (
"io"
"log"
"os"
"os/user"
"path"

"gopkg.in/yaml.v3"
)
Expand Down Expand Up @@ -31,3 +34,20 @@ func parseProfile(filepath string) (*Profile, error) {

return prf, nil
}

func getProfile(profName string) (*Profile, error) {
usr, err := user.Current()
if err != nil {
log.Printf("error: user.Current(). %s", err)
os.Exit(1)
}

var profile = &Profile{}
profPath := path.Join(usr.HomeDir, ".config", "discord-quickpost", profName+".yaml")
profile, err = parseProfile(profPath)
if err != nil {
return nil, err
}

return profile, nil
}
7 changes: 7 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,10 @@ require (
github.com/spf13/pflag v1.0.5
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/bwmarrin/discordgo v0.27.1 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b // indirect
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 // indirect
)
12 changes: 12 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
github.com/bwmarrin/discordgo v0.27.1 h1:ib9AIc/dom1E/fSIulrBwnez0CToJE113ZGt4HoliGY=
github.com/bwmarrin/discordgo v0.27.1/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b h1:7mWr3k41Qtv8XlltBkDkl8LoP3mpSgBW8BUoxtEdbXg=
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 h1:nxC68pudNYkKU6jWhgrqdreuFiOQWj1Fs7T3VrH4Pjw=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
147 changes: 73 additions & 74 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ package main

import (
"fmt"
"log"
"os"
"os/user"
"path"
"runtime/debug"

"github.com/bwmarrin/discordgo"
flag "github.com/spf13/pflag"
)

Expand All @@ -26,64 +24,32 @@ type CliOutput struct {
}

type Options struct {
session *discordgo.Session
token string
text string
filepath string
postOpts *PostOptions

// mode string
// blocks string
// snippetMode bool
// slackClient SlackClient
}

type PostOptions struct {
username string
channel string
// iconEmoji string
// iconUrl string
// threadTs string
}

func strGetFirstOne(vars ...string) string {
for _, v := range vars {
if v != "" {
return v
}
}
return ""
channel string
text string
filePaths []string
}

func main() {
var (
// mode: print version
printVersion = flag.Bool("version", false, "print version")

// mode: post text
optText = flag.String("text", "", "post text")
optTextFile = flag.String("textfile", "", "post text file path")
// snippetMode = flag.Bool("snippet", false, "post text as snippet")
optText = flag.String("text", "", "post text")
optFile = flag.String("file", "", "post file path")

// mode: post file
filepath = flag.String("file", "", "post file path")
envToken = os.Getenv("DISCORD_TOKEN")
optToken = flag.String("token", "", "discord app token")

// mode: post blocks json
// optBlocks = flag.String("blocks", "", "post BlockKit json")

// must options
envToken = os.Getenv("SLACK_TOKEN")
optToken = flag.String("token", "", "slack app OAuth token")
optChannel = flag.String("channel", "", "post slack channel id")

// optional
envProfile = os.Getenv("SLACK_QUICKPOST_PROFILE")
optProfile = flag.String("profile", "", "slack quickpost profile name")
// threadTs = flag.String("thread-ts", "", "post under thread")
// iconEmoji = flag.String("icon", "", "icon emoji")
// iconUrl = flag.String("icon-url", "", "icon image url")
username = flag.String("username", "", "user name")
// envProfile = os.Getenv("DISCORD_QUICKPOST_PROFILE")
// optProfile = flag.String("profile", "", "discord quickpost profile name")

noFail = flag.Bool("nofail", false, "always return success code(0)")
noFail = flag.Bool("nofail", false, "always return success code(0)")
printVersion = flag.Bool("version", false, "print version")

errText []string
)
Expand All @@ -95,44 +61,48 @@ func main() {
}

opts := &Options{
// snippetMode: *snippetMode,
filepath: *filepath,
postOpts: &PostOptions{
username: *username,
// iconEmoji: *iconEmoji,
// iconUrl: *iconUrl,
// threadTs: *threadTs,
},
}
_ = optText
_ = optTextFile
usr, err := user.Current()
if err != nil {
log.Printf("error: user.Current(). %s", err)
os.Exit(1)
postOpts: &PostOptions{},
}

profileName := strGetFirstOne(*optProfile, envProfile)
opts.postOpts.text = *optText

var profile = &Profile{}
if profileName != "" {
profPath := path.Join(usr.HomeDir, ".config", "discord-quickpost", profileName+".yaml")
profile, err = parseProfile(profPath)
if err != nil {
errText = append(errText, fmt.Sprintf("error: failed read profile %s. %s", profPath, err.Error()))
}
}
// var profile = &profile{}
// profileName := strGetFirstOne(*optProfile, envProfile)
// if profileName != "" {
// var err error
// profile, err = getProfile(profileName)
// if err != nil {
// errText = append(errText, fmt.Sprintf("error: failed read profile %s. %s", profPath, err.Error()))
// }
// }

opts.token = strGetFirstOne(*optToken, envToken, profile.Token)
// opts.token = strGetFirstOne(*optToken, envToken, profile.Token)
opts.token = strGetFirstOne(*optToken, envToken)
if opts.token == "" {
errText = append(errText, "error: slack token is required")
}

opts.postOpts.channel = strGetFirstOne(*optChannel, profile.Channel)
// opts.postOpts.channel = strGetFirstOne(*optChannel, profile.Channel)
opts.postOpts.channel = strGetFirstOne(*optChannel)
if opts.postOpts.channel == "" {
errText = append(errText, "error: channel is required")
}

if *optFile != "" {
opts.postOpts.filePaths = []string{*optFile}
}

// Discord initialize
session, err := discordgo.New("Bot " + opts.token)
if err != nil {
fmt.Println(err.Error())
if *noFail {
os.Exit(0)
}
os.Exit(1)
}
opts.session = session

if _, err := Do(opts); err != nil {
fmt.Println(err.Error())
if *noFail {
Expand All @@ -146,6 +116,35 @@ func main() {

func Do(opts *Options) (*CliOutput, error) {
var output *CliOutput
log.Println("Do")
var sendParameter = &discordgo.MessageSend{}
if opts.postOpts.text != "" {
sendParameter.Content = opts.postOpts.text
}

// TODO: 2つ以上に対応する
var f *os.File
if len(opts.postOpts.filePaths) > 0 {
var err error
if f, err = os.Open(opts.postOpts.filePaths[0]); err != nil {
return nil, err
}
defer f.Close()

file := &discordgo.File{
Name: f.Name(),
ContentType: "text/plain",
Reader: f,
}

sendParameter.Files = []*discordgo.File{file}
}

if _, err := opts.session.ChannelMessageSendComplex(
opts.postOpts.channel,
sendParameter,
); err != nil {
return nil, err
}

return output, nil
}
10 changes: 10 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package main

func strGetFirstOne(vars ...string) string {
for _, v := range vars {
if v != "" {
return v
}
}
return ""
}

0 comments on commit bb2d2f6

Please sign in to comment.