Skip to content

Commit

Permalink
Add Environment Variables
Browse files Browse the repository at this point in the history
  • Loading branch information
taras-by committed Dec 14, 2019
1 parent 10d8301 commit 1b6d257
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 16 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
/my.db
/telegram_token
*.db
9 changes: 2 additions & 7 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/taras-by/tbot/store"
"io/ioutil"
"log"
"regexp"
"strconv"
Expand Down Expand Up @@ -33,17 +32,13 @@ var (
)

func server() (err error) {
token, err := ioutil.ReadFile(telegramTokenFile)
if err != nil {
log.Panic(err.Error())
}

bot, err = tgbotapi.NewBotAPI(string(token))
bot, err = tgbotapi.NewBotAPI(Opts.TelegramToken)
if err != nil {
log.Panic(err.Error())
}

storage, err = store.NewStorage()
storage, err = store.NewStorage(Opts.StorePath)
if err != nil {
log.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion show.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

func show() (err error) {

storage, err := store.NewStorage()
storage, err := store.NewStorage(Opts.StorePath)
if err != nil {
log.Fatal(err)
}
Expand Down
9 changes: 4 additions & 5 deletions store/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,20 @@ import (

const (
chatsBucketName = "chats"
DBPath = "my.db"
)

type Storage struct {
db *bolt.DB
}

func NewStorage() (*Storage, error) {
func NewStorage(storePath string) (*Storage, error) {

bdb, err := bolt.Open(DBPath, 0600, nil)
bdb, err := bolt.Open(storePath, 0600, nil)

if err != nil {
return nil, errors.Wrapf(err, "failed to make boltdb for %s", DBPath)
return nil, errors.Wrapf(err, "failed to make boltdb for %s", storePath)
}
log.Printf("Storage opened")
log.Printf("Storage opened in %s", storePath)

bdb.Update(func(tx *bolt.Tx) error {
_, err := tx.CreateBucket([]byte(chatsBucketName))
Expand Down
28 changes: 27 additions & 1 deletion tbot.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,30 @@ import (
"log"
"os"
)

type command struct {
fs *flag.FlagSet
fn func(args []string) error
}

type Options struct {
TelegramToken string
StorePath string
}

var (
Opts = Options{}
)

const (
defaultStorePath = "./bolt.db"
)

func main() {

commands := map[string]command{
"server": serverCmd(),
"show": showCmd(),
"show": showCmd(),
}

fs := flag.NewFlagSet("tbot", flag.ExitOnError)
Expand All @@ -27,6 +41,10 @@ func main() {
os.Exit(1)
}

fs.StringVar(&Opts.TelegramToken, "telegram-token", os.Getenv("TELEGRAM_TOKEN"), "Token for Telegram")
fs.StringVar(&Opts.StorePath, "store-path", getEnv("STORE_PATH", defaultStorePath), "Path for storage")
fs.Parse(os.Args[2:])

if cmd, ok := commands[args[0]]; !ok {
log.Fatalf("Unknown command: %s", args[0])
} else if err := cmd.fn(args[1:]); err != nil {
Expand All @@ -45,3 +63,11 @@ func serverCmd() command {
return server()
}}
}

func getEnv(key string, value string) string {
v := os.Getenv(key)
if v == "" {
return value
}
return v
}

0 comments on commit 1b6d257

Please sign in to comment.