282 lines
7.5 KiB
Go
282 lines
7.5 KiB
Go
package martilq
|
|
|
|
import (
|
|
"fmt"
|
|
"gopkg.in/ini.v1"
|
|
"os"
|
|
"time"
|
|
"strings"
|
|
"strconv"
|
|
)
|
|
|
|
const cSoftwareName = "MARTILQREFERENCE"
|
|
const cSoftwareAuthor = "Meerkat@merebox.com"
|
|
const cSoftwareVersion = "0.0.1"
|
|
const cIniFileName = "martilq.ini"
|
|
const cExpires = "7:0:0"
|
|
const cEncoding = "UTF-8"
|
|
|
|
type configuration struct {
|
|
softwareName string
|
|
|
|
logPath string
|
|
|
|
publisher string
|
|
contactPoint string
|
|
accessLevel string
|
|
license string
|
|
rights string
|
|
tags string
|
|
theme string
|
|
batch string
|
|
batchInc float64
|
|
|
|
title string
|
|
author string
|
|
state string
|
|
version string
|
|
expires string
|
|
encoding string
|
|
|
|
hash bool
|
|
hashAlgorithm string
|
|
signKey_File string
|
|
signKey_Password string
|
|
|
|
proxyName string
|
|
proxyPort int
|
|
proxyUser string
|
|
proxyCredential string
|
|
|
|
loaded bool
|
|
configPath string
|
|
}
|
|
|
|
|
|
func GetSoftwareName() string {
|
|
return cSoftwareName
|
|
}
|
|
|
|
func NewConfiguration() configuration {
|
|
|
|
c := configuration {}
|
|
|
|
c.softwareName = GetSoftwareName()
|
|
|
|
c.title = "documentName"
|
|
c.state = "active"
|
|
c.accessLevel = "Confidential"
|
|
c.rights = "Restricted"
|
|
c.expires = cExpires
|
|
c.encoding = cEncoding
|
|
c.batchInc = 0.001
|
|
|
|
c.hash = true
|
|
c.hashAlgorithm = "SHA256"
|
|
c.loaded = false
|
|
|
|
configPath := findIni()
|
|
if configPath != "" {
|
|
c.LoadConfig(configPath)
|
|
}
|
|
|
|
return c
|
|
}
|
|
|
|
func findIni() string {
|
|
|
|
foundPath := ""
|
|
|
|
// Start wih local and move further out
|
|
if foundPath == "" {
|
|
iniFile := Loadenv("MARTILQ_MARTILQ_INI", "")
|
|
if iniFile != "" {
|
|
_, err := os.Stat(iniFile)
|
|
if err == nil {
|
|
foundPath = iniFile
|
|
}
|
|
}
|
|
}
|
|
|
|
if foundPath == "" {
|
|
_, err := os.Stat(cIniFileName)
|
|
if err == nil {
|
|
foundPath = cIniFileName
|
|
}
|
|
}
|
|
|
|
if foundPath == "" {
|
|
_, err := os.Stat("./config/"+ cIniFileName)
|
|
if err == nil {
|
|
foundPath = "./config/"+ cIniFileName
|
|
}
|
|
}
|
|
|
|
if foundPath == "" {
|
|
userHomeDir, err := os.UserHomeDir()
|
|
if err == nil {
|
|
_, err := os.Stat(userHomeDir+ "/"+ cIniFileName)
|
|
if err == nil {
|
|
foundPath = userHomeDir+ "/"+ cIniFileName
|
|
}
|
|
}
|
|
}
|
|
|
|
return foundPath
|
|
}
|
|
|
|
func (c *configuration) SaveConfig(ConfigPath string) bool {
|
|
|
|
cfgini, _ := ini.LooseLoad("./martilq.ini")
|
|
|
|
cfgini.Section("General").Key("logPath").SetValue (c.logPath)
|
|
|
|
cfgini.Section("MartiLQ").Key("tags").SetValue(c.tags)
|
|
cfgini.Section("MartiLQ").Key("publisher").SetValue(c.publisher)
|
|
cfgini.Section("MartiLQ").Key("contactPoint").SetValue(c.contactPoint)
|
|
cfgini.Section("MartiLQ").Key("accessLevel").SetValue(c.accessLevel)
|
|
cfgini.Section("MartiLQ").Key("rights").SetValue(c.rights)
|
|
cfgini.Section("MartiLQ").Key("license").SetValue(c.license)
|
|
cfgini.Section("MartiLQ").Key("batch").SetValue(c.batch)
|
|
cfgini.Section("MartiLQ").Key("theme").SetValue(c.theme)
|
|
|
|
cfgini.Section("Resources").Key("author").SetValue (c.author)
|
|
cfgini.Section("Resources").Key("title").SetValue (c.title)
|
|
cfgini.Section("Resources").Key("state").SetValue (c.state)
|
|
cfgini.Section("Resources").Key("expires").SetValue (c.expires)
|
|
cfgini.Section("Resources").Key("encoding").SetValue (c.encoding)
|
|
cfgini.Section("Resources").Key("version").SetValue (c.version)
|
|
|
|
cfgini.Section("Hash").Key("hashAlgorithm").SetValue (c.hashAlgorithm)
|
|
cfgini.Section("Hash").Key("signKey_File").SetValue (c.signKey_File)
|
|
cfgini.Section("Hash").Key("signKey_Password").SetValue (c.signKey_Password)
|
|
|
|
cfgini.Section("Network").Key("proxyName").SetValue (c.proxyName)
|
|
cfgini.Section("Network").Key("proxyPort").SetValue (strconv.Itoa(c.proxyPort))
|
|
cfgini.Section("Network").Key("proxyUser").SetValue (c.proxyUser)
|
|
cfgini.Section("Network").Key("proxyCredential").SetValue (c.proxyCredential)
|
|
|
|
err := cfgini.SaveTo(ConfigPath)
|
|
if err != nil {
|
|
WriteLog(fmt.Sprintf("Error saving to '%v'" , ConfigPath))
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
func (c *configuration) LoadConfig(ConfigPath string) bool {
|
|
|
|
if ConfigPath != "" {
|
|
_, err := os.Stat(ConfigPath)
|
|
if os.IsNotExist(err) {
|
|
WriteLog(fmt.Sprintf("Configuration path '%v' does not exist" , ConfigPath))
|
|
return false
|
|
}
|
|
} else {
|
|
// Check default locations
|
|
_, err := os.Stat(cIniFileName)
|
|
if os.IsNotExist(err) == false {
|
|
ConfigPath = cIniFileName
|
|
}
|
|
|
|
}
|
|
|
|
if ConfigPath != "" {
|
|
|
|
cfgini, err := ini.Load(ConfigPath)
|
|
if err != nil {
|
|
WriteLog(fmt.Sprintf("Fail to read file: %v", ConfigPath))
|
|
fmt.Printf("Fail to read file: %v", err)
|
|
return false
|
|
}
|
|
|
|
c.logPath = cfgini.Section("General").Key("logPath").MustString(c.logPath)
|
|
|
|
c.tags = cfgini.Section("MartiLQ").Key("tags").MustString(c.tags)
|
|
c.accessLevel = cfgini.Section("MartiLQ").Key("accessLevel").MustString(c.accessLevel)
|
|
c.rights = cfgini.Section("MartiLQ").Key("rights").MustString(c.rights)
|
|
c.batch = cfgini.Section("MartiLQ").Key("batch").MustString(c.batch)
|
|
c.license = cfgini.Section("MartiLQ").Key("license").MustString(c.license)
|
|
c.publisher = cfgini.Section("MartiLQ").Key("publisher").MustString(c.publisher)
|
|
c.contactPoint = cfgini.Section("MartiLQ").Key("contactPoint").MustString(c.contactPoint)
|
|
c.theme= cfgini.Section("MartiLQ").Key("theme").MustString(c.theme)
|
|
|
|
c.author = cfgini.Section("Resources").Key("title").MustString(c.title)
|
|
c.author = cfgini.Section("Resources").Key("author").MustString(c.author)
|
|
c.state = cfgini.Section("Resources").Key("state").MustString(c.state)
|
|
c.expires = cfgini.Section("Resources").Key("expires").MustString(c.expires)
|
|
c.encoding = cfgini.Section("Resources").Key("encoding").MustString(c.encoding)
|
|
|
|
c.hashAlgorithm = cfgini.Section("Hash").Key("hashAlgorithm").MustString(c.hashAlgorithm)
|
|
c.signKey_File = cfgini.Section("Hash").Key("signKey_File").MustString(c.signKey_File)
|
|
c.signKey_Password = cfgini.Section("Hash").Key("signKey_Password").MustString(c.signKey_Password)
|
|
|
|
c.proxyName = cfgini.Section("Network").Key("proxyName").MustString(c.proxyName)
|
|
port := cfgini.Section("Network").Key("proxyPort").MustString("")
|
|
if port != "" {
|
|
c.proxyPort, _ = strconv.Atoi(port)
|
|
}
|
|
c.proxyUser = cfgini.Section("Network").Key("proxyUser").MustString(c.proxyUser)
|
|
c.proxyCredential = cfgini.Section("Network").Key("proxyCredential").MustString(c.proxyCredential)
|
|
|
|
WriteLog(fmt.Sprintf("Loaded config file: %v", ConfigPath))
|
|
c.configPath = ConfigPath
|
|
}
|
|
|
|
// Now check environmental values
|
|
c.signKey_File = Loadenv("MARTILQ_SIGNKEY_FILE", c.signKey_File)
|
|
c.signKey_Password = Loadenv("MARTILQ_SIGNKEY_PASSWORD", c.signKey_Password)
|
|
|
|
c.logPath = Loadenv("MARTILQ_LOGPATH", c.logPath)
|
|
|
|
c.loaded = true
|
|
|
|
return true
|
|
}
|
|
|
|
|
|
func Loadenv(key string, default_value string ) string {
|
|
|
|
tmp := os.Getenv(key)
|
|
if tmp != "" {
|
|
return tmp
|
|
}
|
|
return default_value
|
|
}
|
|
|
|
|
|
func (c *configuration) ExpireDate( ) time.Time {
|
|
|
|
var expires time.Time
|
|
|
|
lExpires := strings.Split(c.expires,":")
|
|
if len(lExpires) != 3 && len(lExpires) != 6 {
|
|
panic("Expires value '"+ c.expires +"' is invalid")
|
|
}
|
|
|
|
|
|
var lExpire [3]int
|
|
lex, _ := strconv.Atoi(lExpires[0])
|
|
lExpire[0] = lex
|
|
lex, _ =strconv.Atoi(lExpires[1])
|
|
lExpire[1] = lex
|
|
lex, _ =strconv.Atoi(lExpires[2])
|
|
lExpire[2] = lex
|
|
|
|
if len(lExpires) > 3 {
|
|
var lExpireD [3]int
|
|
lex, _ := strconv.Atoi(lExpires[3])
|
|
lExpireD[0] = lex
|
|
lex, _ =strconv.Atoi(lExpires[4])
|
|
lExpireD[1] = lex
|
|
lex, _ =strconv.Atoi(lExpires[5])
|
|
lExpireD[2] = lex
|
|
expires = time.Now().AddDate(lExpire[0],lExpire[1],lExpire[2]).Add(time.Hour * time.Duration(lExpireD[0])).Add(time.Minute * time.Duration(lExpireD[1])).Add(time.Second * time.Duration(lExpireD[2]))
|
|
} else {
|
|
expires = time.Now().AddDate(lExpire[0],lExpire[1],lExpire[2])
|
|
expires = time.Date(expires.Year(), expires.Month(), expires.Day(), 0, 0, 0, 0, time.Local)
|
|
}
|
|
|
|
return expires
|
|
}
|