Skip to content

Commit

Permalink
add config package
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikhail Borovikov committed Nov 7, 2021
1 parent b293e82 commit 08c8108
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519
google.golang.org/api v0.60.0
gopkg.in/tucnak/telebot.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c
)

require (
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,7 @@ gopkg.in/tucnak/telebot.v2 v2.4.0 h1:nOeqOWnOAD3dzbKW+NRumd8zjj5vrWwSa0WRTxvgfag
gopkg.in/tucnak/telebot.v2 v2.4.0/go.mod h1:BgaIIx50PSRS9pG59JH+geT82cfvoJU/IaI5TJdN3v8=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
65 changes: 65 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2021 Mikhail Borovikov and The Secretable Authors
//
// 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 config

import (
"bytes"
"errors"
"io"
"os"
"secretable/pkg/log"

"gopkg.in/yaml.v3"
)

type Config struct {
TelegramBotToken string `yaml:"telegram_bot_token"`
GoogleCredentials string `yaml:"google_credentials_file"`
SpreadsheetID string `yaml:"spreadsheet_id"`
CleanupTimeout int `yaml:"cleanup_timeout"`
Unencrypted bool `yaml:"unencrypted"`
Salt string `yaml:"salt"`
}

func ParseFromFile(path string) (config *Config, err error) {
config = new(Config)
file, err := os.Open(path)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
if file, err = os.Create(path); err != nil {
return nil, err
}
log.Info("📝 Created config file " + path)
} else {
return nil, err
}
}

defer file.Close()

if err = yaml.NewDecoder(file).Decode(config); err != nil && err != io.EOF {
return nil, err
}

return config, nil
}

func UpdateFile(path string, config *Config) error {
buf := bytes.NewBuffer([]byte{})
if err := yaml.NewEncoder(buf).Encode(config); err != nil {
return nil
}
return os.WriteFile(path, buf.Bytes(), os.ModePerm)
}

0 comments on commit 08c8108

Please sign in to comment.