-
Notifications
You must be signed in to change notification settings - Fork 27
/
local.go
97 lines (79 loc) · 2.34 KB
/
local.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package local
/*
Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
*/
import (
"os"
"github.com/mholt/archiver/v3"
"github.com/facebookincubator/go2chef"
"github.com/mitchellh/mapstructure"
"github.com/otiai10/copy"
)
// TypeName is the name of this source plugin
const TypeName = "go2chef.source.local"
// Source implements a local filesystem source plugin that
// copies files from a directory on local filesystem to temp
// for use. Paths are relative to the current working directory
// of the go2chef process.
type Source struct {
logger go2chef.Logger
SourceName string `mapstructure:"name"`
Path string `mapstructure:"path"`
Archive bool `mapstructure:"archive"`
}
func (s *Source) String() string {
return "<" + TypeName + ":" + s.SourceName + ">"
}
// Name returns the name of this source instance
func (s *Source) Name() string {
return s.SourceName
}
// Type returns the type of this source
func (s *Source) Type() string {
return TypeName
}
// SetName sets the name of this source instance
func (s *Source) SetName(name string) {
s.SourceName = name
}
// DownloadToPath performs the actual copy of files to the working directory.
// We copy rather than just setting downloadPath to avoid side effects from
// steps affecting the original source location.
func (s *Source) DownloadToPath(dlPath string) error {
if err := os.MkdirAll(dlPath, 0755); err != nil {
return err
}
s.logger.Debugf(0, "copy directory %s is ready", dlPath)
if !s.Archive {
if err := copy.Copy(s.Path, dlPath); err != nil {
s.logger.Errorf("failed to copy directory %s to %s", s.Path, dlPath)
return err
}
s.logger.Debugf(0, "copied directory %s to %s", s.Path, dlPath)
} else {
if err := archiver.Unarchive(s.Path, dlPath); err != nil {
s.logger.Errorf("failed to unarchive %s to dir %s", s.Path, dlPath)
return err
}
}
return nil
}
// Loader provides an instantiation function for this source
func Loader(config map[string]interface{}) (go2chef.Source, error) {
s := &Source{
logger: go2chef.GetGlobalLogger(),
SourceName: "",
}
if err := mapstructure.Decode(config, s); err != nil {
return nil, err
}
if s.SourceName == "" {
s.SourceName = TypeName
}
return s, nil
}
var _ go2chef.Source = &Source{}
var _ go2chef.SourceLoader = Loader
func init() {
go2chef.RegisterSource(TypeName, Loader)
}