[client:config] updated (eased) config management with 'Store(keyset.T)' and 'Fetch(keyset.T)'

This commit is contained in:
xdrm-brackets 2018-04-22 00:58:38 +02:00
parent 9785e8e573
commit 507c29398d
1 changed files with 54 additions and 49 deletions

View File

@ -1,7 +1,7 @@
package client; package client;
import ( import (
"io" "git.xdrm.io/schastsp/client/keyset"
"fmt" "fmt"
"path/filepath" "path/filepath"
"errors" "errors"
@ -11,7 +11,6 @@ import (
type config struct { type config struct {
path string // absolute configuration file path path string // absolute configuration file path
fd *os.File // file descriptor
} }
/* (1) Constructor /* (1) Constructor
@ -24,61 +23,47 @@ func Config(dir string, name string) (*config, error) {
inst := new(config); inst := new(config);
/* (1) Build full path */
/* (1) Check directory */
absoluteDir, err := filepath.Abs(dir); absoluteDir, err := filepath.Abs(dir);
if err != nil { return nil, err; } if err != nil { return nil, err; }
basename := filepath.Base(name);
fullpath := fmt.Sprintf("%s/%s", absoluteDir, basename);
/* (2) Check directory */
if info, err := os.Stat(absoluteDir); err != nil { if info, err := os.Stat(absoluteDir); err != nil {
// fail if directory does not exist // Unknown error
if os.IsNotExist(err) { if !os.IsNotExist(err) { return nil, err }
// try to create file // not exists -> try to create dir
err2 := os.MkdirAll(absoluteDir, 0755); err2 := os.MkdirAll(absoluteDir, 0755);
if err2 != nil { return nil, err2 }
if err2 != nil {
return nil, errors.New("Cannot create missing configuration dir");
}
// fail if not a directory // fail if not a directory
} else if !info.Mode().IsDir() { } else if !info.Mode().IsDir() {
return nil, errors.New("Configuration dir is not a directory"); return nil, errors.New("Configuration dir is not a directory");
} }
}
/* (2) Build full path */
basename := filepath.Base(name);
fullpath := fmt.Sprintf("%s/%s", absoluteDir, basename);
/* (3) Check file */ /* (3) Check file */
info, err := os.Stat(fullpath); info, err := os.Stat(fullpath);
if err != nil { if err != nil {
// If not exists // Unknown error
if os.IsNotExist(err) { if !os.IsNotExist(err) { return nil, err }
// try to create file // File does not exist -> try to create file
_, err2 := os.Create(fullpath); _, err2 := os.Create(fullpath);
if err2 != nil { return nil, err2 }
if err2 != nil {
return nil, errors.New("Cannot create missing configuration file");
}
}
// fail if exists but not regular file // fail if exists but not regular file
if !info.Mode().IsRegular() { } else if !info.Mode().IsRegular() {
return nil, errors.New("Configuration file is not a regular file"); return nil, errors.New("Configuration file is not a regular file");
} }
}
fmt.Printf("hehe: %t\n", info.Mode().IsRegular());
inst.path = fullpath inst.path = fullpath
return inst, nil return inst, nil
@ -86,33 +71,53 @@ func Config(dir string, name string) (*config, error) {
} }
/* (2) Get Reader on the file /* (2) Fetches a keyset from a file
* *
* @return reader<io.Reader> File reader * @ks<*keyset.T> Set to fetch into
*
* @return err<error> Fetching error
* NIL on error * NIL on error
* *
---------------------------------------------------------*/ ---------------------------------------------------------*/
func (c config) Reader() io.ReadCloser { func (c config) Fetch(ks *keyset.T) (err error) {
file, err := os.Open(c.path); /* (1) Open file */
if err != nil { return nil } file, err := os.Open(c.path)
if err != nil { return err }
return file; /* (2) Defer close */
defer file.Close()
/* (3) Fetch from file */
err = ks.Fetch(file);
if err != nil { return err }
return nil
} }
/* (3) Get Writer on the file /* (3) Stores a keyset into file
* *
* @return reader<io.Writer> File reader * @ks<*keyset.T> Set to store
*
* @return err<error> Storage error
* NIL on error * NIL on error
* *
---------------------------------------------------------*/ ---------------------------------------------------------*/
func (c config) Writer() io.WriteCloser { func (c config) Store(ks *keyset.T) error {
/* (1) Open file */
file, err := os.OpenFile(c.path, os.O_RDWR|os.O_CREATE, 0755); file, err := os.OpenFile(c.path, os.O_RDWR|os.O_CREATE, 0755);
if err != nil { return nil } if err != nil { return err }
return file; /* (2) Defer close */
defer file.Close()
/* (3) Store into file */
err = ks.Store(file)
if err != nil { return err }
return nil
} }