From 12b906eac64818fd24d7e31327522354f140f372 Mon Sep 17 00:00:00 2001 From: James Shubin Date: Thu, 5 Sep 2019 18:17:05 -0400 Subject: [PATCH] engine: Refactor state dir into a separate function This lets us re-use it, and know the path is fixed. --- engine/graph/engine.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/engine/graph/engine.go b/engine/graph/engine.go index bb374b35..788caf99 100644 --- a/engine/graph/engine.go +++ b/engine/graph/engine.go @@ -30,6 +30,12 @@ import ( "github.com/purpleidea/mgmt/util/semaphore" ) +const ( + // StateDir is the name of the sub directory where all the local + // resource state is stored. + StateDir = "state" +) + // Engine encapsulates a generic graph and manages its operations. type Engine struct { Program string @@ -176,7 +182,8 @@ func (obj *Engine) Commit() error { // FIXME: is res.Name() sufficiently unique to use as a UID here? pathUID := fmt.Sprintf("%s-%s", res.Kind(), res.Name()) - statePrefix := fmt.Sprintf("%s/", path.Join(obj.Prefix, "state", pathUID)) + statePrefix := fmt.Sprintf("%s/", path.Join(obj.statePrefix(), pathUID)) + // don't create this unless it *will* be used //if err := os.MkdirAll(statePrefix, 0770); err != nil { // return errwrap.Wrapf(err, "can't create state prefix") @@ -416,3 +423,8 @@ func (obj *Engine) Close() error { func (obj *Engine) Graph() *pgraph.Graph { return obj.graph } + +// statePrefix returns the dir where all the resource state is stored locally. +func (obj *Engine) statePrefix() string { + return fmt.Sprintf("%s/", path.Join(obj.Prefix, StateDir)) +}