[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;
import (
"io"
"git.xdrm.io/schastsp/client/keyset"
"fmt"
"path/filepath"
"errors"
@ -11,7 +11,6 @@ import (
type config struct {
path string // absolute configuration file path
fd *os.File // file descriptor
}
/* (1) Constructor
@ -24,61 +23,47 @@ func Config(dir string, name string) (*config, error) {
inst := new(config);
/* (1) Check directory */
/* (1) Build full path */
absoluteDir, err := filepath.Abs(dir);
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 {
// fail if directory does not exist
if os.IsNotExist(err) {
// Unknown error
if !os.IsNotExist(err) { return nil, err }
// try to create file
err2 := os.MkdirAll(absoluteDir, 0755);
if err2 != nil {
return nil, errors.New("Cannot create missing configuration dir");
}
// fail if not a directory
} else if !info.Mode().IsDir() {
return nil, errors.New("Configuration dir is not a directory");
}
// not exists -> try to create dir
err2 := os.MkdirAll(absoluteDir, 0755);
if err2 != nil { return nil, err2 }
// fail if not a directory
} else if !info.Mode().IsDir() {
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 */
info, err := os.Stat(fullpath);
if err != nil {
// If not exists
if os.IsNotExist(err) {
// Unknown error
if !os.IsNotExist(err) { return nil, err }
// try to create file
_, err2 := os.Create(fullpath);
// File does not exist -> try to create file
_, 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
if !info.Mode().IsRegular() {
return nil, errors.New("Configuration file is not a regular file");
}
// fail if exists but not regular file
} else if !info.Mode().IsRegular() {
return nil, errors.New("Configuration file is not a regular file");
}
fmt.Printf("hehe: %t\n", info.Mode().IsRegular());
inst.path = fullpath
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
*
---------------------------------------------------------*/
func (c config) Reader() io.ReadCloser {
func (c config) Fetch(ks *keyset.T) (err error) {
file, err := os.Open(c.path);
if err != nil { return nil }
/* (1) Open file */
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
*
---------------------------------------------------------*/
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);
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
}