grim/convey
Clone
Summary
Browse
Changes
Graph
Bump the version for release
v0.14.0-alpha3
2018-02-20, Gary Kramlich
166a6d1979fa
Bump the version for release
// Convey
// Copyright 2016-2018 Gary Kramlich <grim@reaperworld.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package
config
import
(
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"bitbucket.org/rw_grim/convey/state"
)
// Loader defines all the functions that a config loader needs to implement.
type
Loader
interface
{
Load
(
path
,
base
string
,
data
[]
byte
,
options
[]
string
,
disableDeprecated
bool
)
(
*
Config
,
error
)
LoadOverride
(
path
,
base
string
,
data
[]
byte
,
config
*
Config
,
disableDeprecated
bool
)
Filenames
()
[]
string
OverrideSuffix
()
string
DefaultPlan
()
string
ResolvePlanName
(
plan
string
,
cfg
*
Config
,
st
*
state
.
State
)
string
}
func
determineFilename
(
filename
string
,
loader
Loader
)
string
{
ext
:=
filepath
.
Ext
(
filename
)
base
:=
strings
.
TrimSuffix
(
filename
,
ext
)
return
base
+
loader
.
OverrideSuffix
()
+
ext
}
func
loadOverride
(
path
,
base
string
,
loader
Loader
,
cfg
*
Config
,
disableDeprecated
bool
)
{
overrideFilename
:=
determineFilename
(
base
,
loader
)
absName
:=
filepath
.
Join
(
path
,
overrideFilename
)
if
_
,
err
:=
os
.
Stat
(
absName
);
os
.
IsNotExist
(
err
)
{
// no override so bail
return
}
data
,
err
:=
ioutil
.
ReadFile
(
absName
)
if
err
!=
nil
{
fmt
.
Printf
(
"error in override file '%s' : %s"
,
absName
,
err
)
return
}
loader
.
LoadOverride
(
path
,
base
,
data
,
cfg
,
disableDeprecated
)
}
// LoadFile will determine the file name and override file name and then
// call the loaders methods to load them.
func
LoadFile
(
file
string
,
loader
Loader
,
options
[]
string
,
disableDeprecated
bool
)
(
*
Config
,
error
)
{
// split the filename into our parts
path
,
base
:=
filepath
.
Split
(
file
)
data
,
err
:=
ioutil
.
ReadFile
(
file
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"failed to read config file '%s'"
,
file
)
}
cfg
,
err
:=
loader
.
Load
(
path
,
base
,
data
,
options
,
disableDeprecated
)
if
err
!=
nil
{
return
cfg
,
err
}
loadOverride
(
path
,
base
,
loader
,
cfg
,
disableDeprecated
)
return
cfg
,
nil
}