Skip to content

Commit

Permalink
- Fix write database in built version
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Choquet committed Mar 22, 2024
1 parent 14bb5b1 commit d7450dd
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 12 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
GITHUB_TOKEN: ${{ secrets.TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ./file-system-service.swagger.yml
asset_path: ./swagger/swagger.yaml

create_dist:
name: Create dist directory
Expand All @@ -43,7 +43,7 @@ jobs:
- name: Create directory
run: mkdir ./dist

release-linux-gui-amd64:
release-linux-amd64:
name: release linux/amd64
runs-on: ubuntu-latest
steps:
Expand All @@ -57,7 +57,7 @@ jobs:
binary_name: file-system-service-linux
retry: 3

release-windows-gui-amd64:
release-windows-amd64:
name: release windows/amd64
runs-on: ubuntu-latest
steps:
Expand All @@ -71,7 +71,7 @@ jobs:
binary_name: file-system-service-windows
retry: 3

release-darwin-gui-amd64:
release-darwin-amd64:
name: release darwin/amd64
runs-on: ubuntu-latest
steps:
Expand All @@ -85,7 +85,7 @@ jobs:
binary_name: file-system-service-darwin-amd64
retry: 3

release-darwin-gui-arm64:
release-darwin-arm64:
name: release darwin/arm64
runs-on: ubuntu-latest
steps:
Expand Down
35 changes: 29 additions & 6 deletions database/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,30 @@ package database

import (
"database/sql"
"filesystem_service/flags"
"fmt"
"os"
"strings"
)

var databaseFileName = "file-system-service-oauth.sqlite"

func checkFatalPermissionDenied(err error) {
if strings.Contains(err.Error(), "readonly database") {
fmt.Printf("La base de données est en lecture seule car vous n'avez pas les droit d'écriture.\n")
fmt.Printf("Lancer le service en sudo pour avoir les droits d'écriture.\n\n")
fmt.Printf("sudo %v\n", strings.Join(os.Args, " "))
os.Exit(2)
}
}

func Connect() (*sql.DB, error) {
return sql.Open("sqlite", "./file-system-service-oauth.sqlite")
path := "."
if flags.IsProd() {
path = prodPath
}

return sql.Open("sqlite", path+"/"+databaseFileName)
}

func Init() (*sql.DB, error) {
Expand All @@ -30,6 +50,7 @@ func Init() (*sql.DB, error) {
(7, 'delete_file'),
(8, 'rename_file'),
(9, 'update_file_content');`); err != nil {
checkFatalPermissionDenied(err)
return nil, err
}

Expand All @@ -43,15 +64,16 @@ func Init() (*sql.DB, error) {
if _, err = db.Exec(`INSERT OR IGNORE INTO roles (id, role_name, active) VALUES
(1, 'readwrite', TRUE),
(2, 'readonly', TRUE);`); err != nil {
checkFatalPermissionDenied(err)
return nil, err
}

if _, err = db.Exec(`CREATE TABLE IF NOT EXISTS roles_link_role_actions (
role_name VARCHAR(255) NOT NULL,
role_action_name VARCHAR(255) NOT NULL,
PRIMARY KEY (role_name, role_action_name),
FOREIGN KEY (role_name) REFERENCES roles(role_name),
FOREIGN KEY (role_action_name) REFERENCES role_actions(role_action_name)
role INTEGER NOT NULL,
role_action INTEGER NOT NULL,
PRIMARY KEY (role, role_action),
FOREIGN KEY (role) REFERENCES roles(id),
FOREIGN KEY (role_action) REFERENCES role_actions(id)
);`); err != nil {
return nil, err
}
Expand All @@ -67,6 +89,7 @@ func Init() (*sql.DB, error) {
(1, 9),
(2, 1),
(2, 5);`); err != nil {
checkFatalPermissionDenied(err)
return nil, err
}

Expand Down
12 changes: 12 additions & 0 deletions database/main_darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package database

import "os"

var prodPath = func() string {
path := "/etc/filesystem-service"
_, err := os.Stat(path)
if err != nil {
_ = os.MkdirAll(path, 0777)
}
return path
}()
12 changes: 12 additions & 0 deletions database/main_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package database

import "os"

var prodPath = func() string {
path := "/etc/filesystem-service"
_, err := os.Stat(path)
if err != nil {
_ = os.MkdirAll(path, 0777)
}
return path
}()
12 changes: 12 additions & 0 deletions database/main_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package database

import "os"

var prodPath = func() string {
path := "C://Program Files/filesystem-service"
_, err := os.Stat(path)
if err != nil {
_ = os.MkdirAll(path, 0777)
}
return path
}()
18 changes: 17 additions & 1 deletion flags/getFlags.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package flags

import "flag"
import (
"flag"
"os"
"regexp"
)

var port = flag.Int("port", 3000, "Port d'exposition de l'application.")
var host = flag.String("host", "127.0.0.1", "Domaine ou IP de la machine qui expose le sercice.")
Expand All @@ -16,6 +20,14 @@ var showRoles = flag.Bool("show-roles", false, "Afficher la liste des rôles dis
var role = flag.String("role", "readonly", "Rensègne le rôle selectionné.")
var clientId = flag.String("client_id", "", "Rensègne le client_id.")

var isBuiltVersion = func() bool {
isMatch := true
for _, _ = range regexp.MustCompile(`(?m)/go-build[0-9]*`).FindAllString(os.Args[0], -1) {
isMatch = false
}
return isMatch
}()

func GetFlags() Flags {
if !flag.Parsed() {
flag.Parse()
Expand All @@ -34,3 +46,7 @@ func GetFlags() Flags {
clientId,
}
}

func IsProd() bool {
return isBuiltVersion
}

0 comments on commit d7450dd

Please sign in to comment.