Skip to content

Commit

Permalink
chore: setup dependencies and db on first usage
Browse files Browse the repository at this point in the history
  • Loading branch information
DeeStarks committed Aug 9, 2022
1 parent e7c9db4 commit e333501
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
8 changes: 4 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ const (
var (
// File System
FS_ROOT string
PROCESS_DB string // DB for app processes
DEFAULT_DB string // Database to use by default
)

func init() {
switch runtime.GOOS {
case "windows":
FS_ROOT = os.ExpandEnv(`C:\Program Files\Conoid\`)
PROCESS_DB = FS_ROOT + `AppProcesses.db`
DEFAULT_DB = FS_ROOT + `Default.db`
case "darwin":
FS_ROOT = os.ExpandEnv(`$HOME/Library/Conoid/`)
PROCESS_DB = FS_ROOT + `AppProcesses.db`
DEFAULT_DB = FS_ROOT + `Default.db`
default:
FS_ROOT = os.ExpandEnv(`/var/lib/conoid/`)
PROCESS_DB = FS_ROOT + `app_processes.db`
DEFAULT_DB = FS_ROOT + `default.db`
}
}
29 changes: 18 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import (

"github.com/DeeStarks/conoid/cmd"
"github.com/DeeStarks/conoid/config"
"github.com/DeeStarks/conoid/utils"
)

// Setup filesystem during new installation
func SetupFs() {
// Create and setup dependencies
func SetupDeps() {
// 1. Setup the data root directory
if _, err := os.Stat(config.FS_ROOT); os.IsNotExist(err) {
err := os.Mkdir(config.FS_ROOT, os.ModePerm)
if err != nil {
Expand All @@ -18,23 +20,28 @@ func SetupFs() {
}
}

// Create dependency files
depFiles := []string{
config.PROCESS_DB,
}
for _, dep := range depFiles {
f, err := os.OpenFile(dep, os.O_RDONLY|os.O_CREATE, 0644)
// 2. Create the default database
if _, err := os.Stat(config.DEFAULT_DB); os.IsNotExist(err) {
// Create if id does not exist
f, err := os.OpenFile(config.DEFAULT_DB, os.O_CREATE, 0644)
if err != nil {
log.Printf("Error creating dependency file: %s; Error: %v\n", dep, err)
log.Printf("Error creating file: %s; Error: %v\n", config.DEFAULT_DB, err)
return
}
f.Close()

// Migrate schemadb
err = utils.Sqlite3Migrate(config.DEFAULT_DB, "./domain/schemas/default.sql")
if err != nil {
log.Println("Error migrating schema:", err)
return
}
}
}

func main() {
// Setup filesystem
SetupFs()
// Setup dependencies during installation
SetupDeps()

// Start process based on command argument
cmd.Execute()
Expand Down

0 comments on commit e333501

Please sign in to comment.