grim/convey

We need to clean up the workspace directory even while testing
// Convey
// Copyright 2016-2019 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 workspace
import (
"io/ioutil"
"os"
"path/filepath"
log "github.com/sirupsen/logrus"
)
type Workspace struct {
parent string
root string
path string
volumePath string
}
func New(parent string) (*Workspace, error) {
root := filepath.Join(parent, ".convey")
err := os.MkdirAll(root, 0700)
if err != nil {
return nil, err
}
path, err := ioutil.TempDir(root, "")
if err != nil {
return nil, err
}
volumePath := filepath.Join(path, "volume")
err = os.Mkdir(volumePath, 0700)
if err != nil {
return nil, err
}
workspace := &Workspace{
parent: parent,
root: root,
path: path,
volumePath: volumePath,
}
return workspace, nil
}
func (ws *Workspace) Destroy() {
if err := os.RemoveAll(ws.path); err != nil {
log.Errorf("error removing workspace: %s", err)
return
}
// try to remove the .convey directory. This will only succeed when
// our state is the only one and that's intentional.
if err := os.Remove(ws.root); err != nil {
log.Errorf("error removing %s: %s", ws.root, err)
}
}
func (ws *Workspace) Path() string {
return ws.path
}
func (ws *Workspace) Volume() string {
return ws.volumePath
}