1
0

5 Revīzijas 8e0881ac8f ... b15406cccf

Autors SHA1 Ziņojums Datums
  Banana b15406cccf go client edit and edit list commands 1 mēnesi atpakaļ
  Banana b5826cc112 added config change to validation 1 mēnesi atpakaļ
  Banana 39e47617f1 changed endpoint file name 1 mēnesi atpakaļ
  Banana 98ed531f57 online first. offline and sync later 2 mēneši atpakaļ
  Banana 62228a807d changed to github.com/adrg/xdg 2 mēneši atpakaļ

+ 1 - 0
TODO

@@ -3,3 +3,4 @@
 + PHP better logging
 + PHP Better API
 + Go client improvements
+++ local storage and sync

+ 19 - 2
client/go-cli/scientia/cmd/add.go

@@ -12,13 +12,30 @@ import (
 	Helper "scientia/lib"
 )
 
+
+/**
+ * scientia
+ *
+ * Copyright 2023 - 2024 Johannes Keßler
+ *
+ * https://www.bananas-playground.net/projekt/scientia/
+ *
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
+ *
+ * You should have received a copy of the
+ * COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
+ * along with this program.  If not, see http://www.sun.com/cddl/cddl.html
+ */
+
 func init() {
 	rootCmd.AddCommand(addCmd)
 }
 
 var addCmd = &cobra.Command {
 	Use: "add file.ext",
-	Short: "Add a new entry and return the URL",
+	Short: "Add a new entry and get the URL returned",
 	Long: "Add a new entry based on a file or piped cat file | scientia add. Returns the url to the new entry.",
 	Run: func(cmd *cobra.Command, args []string) {
 		// check if there is a file or piped content
@@ -80,7 +97,7 @@ func upload(payload string) Response {
 	jsonData, err := json.Marshal(payloadStruct)
 	Helper.ErrorCheck(err, "Can not create json payload")
 
-	req, err := http.NewRequest(http.MethodPost, ScientiaConfig.Endpoint.Url, bytes.NewBuffer(jsonData))
+	req, err := http.NewRequest(http.MethodPost, ScientiaConfig.Endpoint.Add, bytes.NewBuffer(jsonData))
 	Helper.ErrorCheck(err, "Can not create http request")
 	// We need to set the content type from the writer, it includes necessary boundary as well
 	req.Header.Set("Content-Type", "application/json; charset=UTF-8")

+ 16 - 0
client/go-cli/scientia/cmd/config.go

@@ -5,6 +5,22 @@ import (
 	"os"
 )
 
+/**
+ * scientia
+ *
+ * Copyright 2023 - 2024 Johannes Keßler
+ *
+ * https://www.bananas-playground.net/projekt/scientia/
+ *
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
+ *
+ * You should have received a copy of the
+ * COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
+ * along with this program.  If not, see http://www.sun.com/cddl/cddl.html
+ */
+
 var configCmd = &cobra.Command {
 	Use: "config",
 	Short: "Modify config",

+ 9 - 5
client/go-cli/scientia/cmd/config_init.go

@@ -3,7 +3,6 @@ package cmd
 import (
 	"errors"
 	"fmt"
-	"github.com/kirsle/configdir"
 	"github.com/spf13/cobra"
 	"os"
 	Helper "scientia/lib"
@@ -25,6 +24,8 @@ import (
  * along with this program.  If not, see http://www.sun.com/cddl/cddl.html
  */
 
+// Subcommand of config
+// to init the config with default values.
 
 func init() {
 	configCmd.AddCommand(configInitCmd)
@@ -43,12 +44,14 @@ var configInitCmd = &cobra.Command {
 
 // initConfig which creates the default config file
 func initConfig() {
-	err := configdir.MakePath(ScientiaConfigPath) // Ensure it exists.
-	Helper.ErrorCheck(err, "No $HOME/.config/scientia directory available?")
+	if _, err := os.Stat(ScientiaConfigPath); os.IsNotExist(err) {
+		err := os.MkdirAll(ScientiaConfigPath, os.ModePerm);
+		Helper.ErrorCheck(err, "No $HOME/.config/scientia directory available?")
+	}
 
 	if FlagDebug {
 		fmt.Printf("DEBUG Local user config path: %s\n", ScientiaConfigPath)
-		fmt.Printf("DEBUG Local user config file: %s\n", ScientiaConfigPath)
+		fmt.Printf("DEBUG Local user config file: %s\n", ScientiaConfigFile)
 	}
 
 	if _, err := os.Stat(ScientiaConfigFile); errors.Is(err, os.ErrNotExist) {
@@ -64,7 +67,8 @@ func initConfig() {
 		fmt.Fprintf(newConfig, "# See %s for more details.\n", Helper.Website)
 		fmt.Fprintf(newConfig, "# Version: %s\n", Helper.Version)
 		fmt.Fprintf(newConfig, "endpoint:\n")
-		fmt.Fprintf(newConfig, "  url: \"http://your-scientia-endpoi.nt/api.php\"\n")
+		fmt.Fprintf(newConfig, "  add: \"http://your-scientia-endpoi.nt/add.php\"\n")
+		fmt.Fprintf(newConfig, "  get: \"http://your-scientia-endpoi.nt/get.php\"\n")
 		fmt.Fprintf(newConfig, "  secret: \"%s\"\n", Helper.RandStringBytes(50))
 
 		fmt.Println("Created a new default config file. Please use the edit command to update it with your settings.")

+ 3 - 3
client/go-cli/scientia/cmd/config_read.go

@@ -25,13 +25,13 @@ import (
  * along with this program.  If not, see http://www.sun.com/cddl/cddl.html
  */
 
+// Subcommand of config
+// to read the config file
+
 func init() {
 	configCmd.AddCommand(configReadCmd)
 }
 
-// Subcommand of config
-// to read the config file
-
 var configReadCmd = &cobra.Command {
 	Use:   "read",
 	Short: "Read config file",

+ 38 - 0
client/go-cli/scientia/cmd/edit.go

@@ -0,0 +1,38 @@
+package cmd
+
+import (
+	"github.com/spf13/cobra"
+	"os"
+)
+
+/**
+ * scientia
+ *
+ * Copyright 2023 - 2024 Johannes Keßler
+ *
+ * https://www.bananas-playground.net/projekt/scientia/
+ *
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
+ *
+ * You should have received a copy of the
+ * COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
+ * along with this program.  If not, see http://www.sun.com/cddl/cddl.html
+ */
+
+var editCmd = &cobra.Command {
+	Use: "edit",
+	Short: "Modify an entry",
+	Long: "Edit an existing entry.",
+	Run: func(cmd *cobra.Command, args []string) {
+		if len(args) == 0 {
+			cmd.Help()
+			os.Exit(0)
+		}
+	},
+}
+
+func init() {
+	rootCmd.AddCommand(editCmd)
+}

+ 45 - 0
client/go-cli/scientia/cmd/edit_list.go

@@ -0,0 +1,45 @@
+package cmd
+
+import (
+	"fmt"
+	"github.com/spf13/cobra"
+)
+
+/**
+ * scientia
+ *
+ * Copyright 2023 - 2024 Johannes Keßler
+ *
+ * https://www.bananas-playground.net/projekt/scientia/
+ *
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
+ *
+ * You should have received a copy of the
+ * COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
+ * along with this program.  If not, see http://www.sun.com/cddl/cddl.html
+ */
+
+
+// Subcommand of edit
+// to list all available entries
+
+var editListCmd = &cobra.Command {
+	Use:   "list",
+	Short: "List all available entries",
+	Long:  "List all available entries",
+	Run: func(cmd *cobra.Command, args []string) {
+		listEntries()
+	},
+}
+
+func init() {
+	editCmd.AddCommand(editListCmd)
+}
+
+func listEntries() {
+	if FlagVerbose {
+		fmt.Println("Starting to request entries")
+	}
+}

+ 0 - 47
client/go-cli/scientia/cmd/pull.go

@@ -1,47 +0,0 @@
-package cmd
-
-import (
-	"fmt"
-	"github.com/spf13/cobra"
-	"io"
-	"os"
-	Helper "scientia/lib"
-)
-
-func init() {
-	rootCmd.AddCommand(pullCmd)
-}
-
-var pullCmd = &cobra.Command {
-	Use: "pull",
-	Short: "Download",
-	Long: "Add a new entry based on a file or piped cat file | scientia add. Returns the url to the new entry.",
-	Run: func(cmd *cobra.Command, args []string) {
-		// check if there is a file or piped content
-		var inputString string
-
-		if len(args) == 1 {
-			if FlagVerbose {
-				fmt.Println("Read from file argument")
-			}
-			bytes, err := os.ReadFile(args[0])
-			Helper.ErrorCheck(err, "Error opening file")
-			inputString = string(bytes)
-		} else if stat, _ := os.Stdin.Stat(); (stat.Mode() & os.ModeCharDevice) == 0  {
-			if FlagVerbose {
-				fmt.Println("Read from piped stdin")
-			}
-			bytes, _ := io.ReadAll(os.Stdin)
-			inputString = string(bytes)
-		} else {
-			cmd.Help()
-			os.Exit(0)
-		}
-
-		response := upload(inputString)
-
-		// print the result and link to the pasty
-		fmt.Printf("Status: %d\n", response.Status)
-		fmt.Printf("Message: %s\n", response.Message)
-	},
-}

+ 9 - 7
client/go-cli/scientia/cmd/root.go

@@ -3,7 +3,7 @@ package cmd
 import (
 	"errors"
 	"fmt"
-	"github.com/kirsle/configdir"
+	"github.com/adrg/xdg"
 	"github.com/spf13/cobra"
 	"gopkg.in/yaml.v3"
 	"log"
@@ -20,7 +20,8 @@ var FlagDebug bool
 // ConfigStruct file struct
 type ConfigStruct struct {
 	Endpoint struct {
-		Url   string `yaml:"url"`
+		Add   string `yaml:"add"`
+		Get   string `yaml:"get"`
 		Secret string `yaml:"secret"`
 	} `yaml:"endpoint"`
 }
@@ -28,8 +29,8 @@ type ConfigStruct struct {
 // The ScientiaConfig used globally
 var ScientiaConfig ConfigStruct
 
-var ScientiaConfigPath = configdir.LocalConfig("scientia")
-var ScientiaConfigFile = ScientiaConfigPath + "/.scientia.yaml"
+var ScientiaConfigPath = xdg.ConfigHome + "/scientia"
+var ScientiaConfigFile = ScientiaConfigPath + "/scientia.yaml"
 
 // The rootCmd
 var rootCmd = &cobra.Command{
@@ -82,12 +83,13 @@ func loadConfig() {
 	err = decoder.Decode(&ScientiaConfig)
 	Helper.ErrorCheck(err, "Can not decode config file")
 
-	if ScientiaConfig.Endpoint.Url == "" || ScientiaConfig.Endpoint.Secret == "" {
-		log.Fatal("Empty config?")
+	if ScientiaConfig.Endpoint.Add == "" || ScientiaConfig.Endpoint.Get == "" || ScientiaConfig.Endpoint.Secret == "" {
+		log.Fatal("Empty or outdated config?")
 	}
 
 	if FlagDebug {
-		fmt.Println("DEBUG Endpoint: " + ScientiaConfig.Endpoint.Url)
+		fmt.Println("DEBUG Add endpoint: " + ScientiaConfig.Endpoint.Add)
+		fmt.Println("DEBUG Get endpoint: " + ScientiaConfig.Endpoint.Get)
 		fmt.Println("DEBUG Secret: " + ScientiaConfig.Endpoint.Secret)
 	}
 }

+ 2 - 1
client/go-cli/scientia/go.mod

@@ -10,11 +10,12 @@ require (
 )
 
 require (
+	github.com/adrg/xdg v0.5.0 // indirect
 	github.com/inconshreveable/mousetrap v1.1.0 // indirect
 	github.com/k0kubun/pp/v3 v3.2.0 // indirect
 	github.com/mattn/go-colorable v0.1.13 // indirect
 	github.com/mattn/go-isatty v0.0.16 // indirect
 	github.com/spf13/pflag v1.0.5 // indirect
-	golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab // indirect
+	golang.org/x/sys v0.22.0 // indirect
 	golang.org/x/text v0.3.7 // indirect
 )

+ 4 - 0
client/go-cli/scientia/go.sum

@@ -1,3 +1,5 @@
+github.com/adrg/xdg v0.5.0 h1:dDaZvhMXatArP1NPHhnfaQUqWBLBsmx1h1HXQdMoFCY=
+github.com/adrg/xdg v0.5.0/go.mod h1:dDdY4M4DF9Rjy4kHPeNL+ilVF+p2lK8IdM9/rTSGcI4=
 github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
 github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
 github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
@@ -16,6 +18,8 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
 github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
 golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab h1:2QkjZIsXupsJbJIdSjjUOgWK3aEtzyuh2mPt3l/CkeU=
 golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
+golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
 golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
 golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
 gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=

+ 7 - 0
documentation/scructure/structure-add.d2

@@ -0,0 +1,7 @@
+add_param: file or stdin
+add_param.shape: oval
+server: server
+server.shape: cloud
+
+add -> add_param
+add_param -> server

+ 10 - 0
documentation/scructure/structure-edit.d2

@@ -0,0 +1,10 @@
+edit_list: list
+edit_id: id
+
+server: server
+server.shape: cloud
+
+edit -> edit_list
+edit -> edit_id
+edit_list <-> server
+edit_id <-> server

+ 20 - 0
documentation/scructure/structure-main.d2

@@ -0,0 +1,20 @@
+sc: scientia
+cfg: config
+cfg_edit: edit
+cfg_init: init
+cfg_read: read
+add: add
+edit: edit
+edit_id: id
+edit_list: list
+
+sc -> cfg
+cfg -> cfg_init
+cfg -> cfg_edit
+cfg -> cfg_read
+
+sc -> add
+
+sc -> edit
+edit -> edit_id
+edit -> edit_list

+ 0 - 69
documentation/structure.d2

@@ -1,69 +0,0 @@
-sc: scientia
-cfg: config
-cfg_edit: edit
-cfg_init: init
-cfg_read: read
-add: add
-pull: pull
-push: push
-edit: edit
-
-sc -> cfg
-cfg -> cfg_init
-cfg -> cfg_edit
-cfg -> cfg_read
-
-sc -> add
-
-addContainer: add {
-    add_param: file or stdin
-    add_param.shape: oval
-    server: server
-    server.shape: cloud
-    local: local
-    local.shape: page
-
-    add -> add_param
-    add_param -> server
-    server -> local
-    add_param -> local: offline
-}
-
-sc -> pull
-
-pullContainer: pull {
-    server: server
-    server.shape: cloud
-    local: local
-    local.shape: page
-
-    pull -> server
-    server -> local: versions
-}
-
-sc -> push
-
-pushContainer: push {
-    server: server
-    server.shape: cloud
-    local: local
-    local.shape: page
-
-    push -> local
-    local -> server: versions
-}
-
-sc -> edit
-
-editContainer: edit {
-    local: local
-    local.shape: page
-
-    edit_list: list
-    edit_id: id
-
-    edit -> edit_list
-    edit -> edit_id
-
-    edit_id -> local: new version
-}

+ 4 - 1
webroot/api.php → webroot/add.php

@@ -15,6 +15,10 @@
  * along with this program.  If not, see http://www.sun.com/cddl/cddl.html
  */
 
+/**
+ * add endpoint. Accepts only POST and valid post body as json
+ */
+
 mb_http_output('UTF-8');
 mb_internal_encoding('UTF-8');
 ini_set('error_reporting',-1); // E_ALL & E_STRICT
@@ -47,7 +51,6 @@ date_default_timezone_set(TIMEZONE);
 # required libs
 require_once('lib/summoner.class.php');
 
-
 if(DEBUG) error_log("Dump SERVER ".var_export($_SERVER,true));
 ## check if request is valid
 $_create = false;

+ 52 - 0
webroot/get.php

@@ -0,0 +1,52 @@
+<?php
+/**
+ * scientia
+ *
+ * Copyright 2023 - 2024 Johannes Keßler
+ *
+ * https://www.bananas-playground.net/projekt/scientia/
+ *
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
+ *
+ * You should have received a copy of the
+ * COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
+ * along with this program.  If not, see http://www.sun.com/cddl/cddl.html
+ */
+
+/**
+ * get endpoint. Accepts only GET and returns data
+ */
+
+mb_http_output('UTF-8');
+mb_internal_encoding('UTF-8');
+ini_set('error_reporting',-1); // E_ALL & E_STRICT
+
+## check request
+$_urlToParse = filter_var($_SERVER['QUERY_STRING'],FILTER_UNSAFE_RAW, FILTER_FLAG_STRIP_LOW);
+if(!empty($_urlToParse)) {
+    # see http://de2.php.net/manual/en/regexp.reference.unicode.php
+    if(preg_match('/[\p{C}\p{M}\p{Sc}\p{Sk}\p{So}\p{Zl}\p{Zp}]/u',$_urlToParse) === 1) {
+        die('Malformed request. Make sure you know what you are doing.');
+    }
+}
+
+## config
+require_once('config/config.php');
+
+## set the error reporting
+ini_set('log_errors',true);
+ini_set('error_log',PATH_SYSTEMOUT.'/error.log');
+if(DEBUG === true) {
+    ini_set('display_errors',true);
+}
+else {
+    ini_set('display_errors',false);
+}
+
+# time settings
+date_default_timezone_set(TIMEZONE);
+
+# required libs
+require_once('lib/summoner.class.php');