Skip to content
Snippets Groups Projects
Commit 44e03bae authored by Tamer Tas's avatar Tamer Tas
Browse files

Use boltdb for appdata

parent 97b2695b
No related branches found
No related tags found
No related merge requests found
package tmplt
import (
"os"
"os/exec"
"path/filepath"
"github.com/tmrts/tmplt/pkg/util/exit"
"github.com/tmrts/tmplt/pkg/util/inpututil"
"github.com/tmrts/tmplt/pkg/util/osutil"
)
const (
Version = "0.0.1"
)
var (
ConfigPath = ".tmpltrc"
Identifier = "tmplt"
DotIdentifier = "." + Identifier
AppDataDirPath = filepath.Join("/var/lib", Identifier)
DBPath = filepath.Join(AppDataDirPath, "config.db")
ConfigDirPath = ".tmplt/"
TemplateDirPath = ".tmplt/templates/"
ConfigPath = DotIdentifier + "rc"
ConfigDirPath = DotIdentifier
TemplateDirPath = filepath.Join(DotIdentifier, "templates")
)
func TemplatePath(name string) (string, error) {
......@@ -21,10 +35,42 @@ func TemplatePath(name string) (string, error) {
func init() {
homeDir, err := osutil.GetUserHomeDir()
if err != nil {
panic(err)
exit.Error(err)
}
ConfigPath = filepath.Join(homeDir, ConfigPath)
ConfigDirPath = filepath.Join(homeDir, ConfigDirPath)
TemplateDirPath = filepath.Join(homeDir, TemplateDirPath)
if err := osutil.DirExists(TemplateDirPath); os.IsNotExist(err) {
shouldInitialize, err := inpututil.ScanYesOrNo("Template directory doesn't exist. Initialize?", true)
if err != nil {
exit.Error(err)
}
if shouldInitialize {
if err := Initialize(); err != nil {
exit.Error(err)
}
} else {
exit.Error(ErrUninitializedTmpltDir)
}
} else if err != nil {
exit.Error(err)
}
}
func Initialize() error {
dirs := []string{
DBPath,
TemplateDirPath,
}
for _, path := range dirs {
if _, err := exec.Command("/usr/bin/mkdir", "-p", path).Output(); err != nil {
return err
}
}
return initializeDB()
}
package tmplt
import (
"os"
"time"
"github.com/boltdb/bolt"
"github.com/tmrts/tmplt/pkg/util/exit"
"github.com/tmrts/tmplt/pkg/util/osutil"
)
type Configuration struct {
ConfigPath string
ConfigDirPath string
}
func initializeDB() error {
if err := osutil.FileExists(DBPath); !os.IsNotExist(err) {
return err
}
db, err := bolt.Open(DBPath, 0600, &bolt.Options{Timeout: 1 * time.Second})
if err != nil {
return err
} else {
defer db.Close()
}
return db.Update(func(tx *bolt.Tx) error {
_ = tx.Bucket([]byte("MyBucket"))
return nil
})
}
func readConfig() (Configuration, error) {
db, err := bolt.Open(DBPath, 0600, &bolt.Options{Timeout: 1 * time.Second})
if err != nil {
exit.Error(err)
} else {
defer db.Close()
}
var conf Configuration
err = db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("MyBucket"))
conf.ConfigPath = string(b.Get([]byte("configPath")))
conf.ConfigDirPath = string(b.Get([]byte("configDirPath")))
return nil
})
return conf, err
}
package tmplt
import "errors"
var (
ErrTemplateAlreadyExists = errors.New("tmplt: project template already exists")
ErrUninitializedTmpltDir = errors.New("tmplt: .tmplt directory is not initialized")
)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment