File: unix.go

package info (click to toggle)
golang-github-juju-utils 0.0~git20171220.f38c0b0-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,748 kB
  • sloc: makefile: 20
file content (133 lines) | stat: -rw-r--r-- 2,960 bytes parent folder | download | duplicates (2)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright 2015 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.

package shell

import (
	"fmt"
	"os"
	"time"

	"github.com/juju/utils"
	"github.com/juju/utils/filepath"
)

// unixRenderer is the base shell renderer for "unix" shells.
type unixRenderer struct {
	filepath.UnixRenderer
}

// Quote implements Renderer.
func (unixRenderer) Quote(str string) string {
	// This *may* not be correct for *all* unix shells...
	return utils.ShQuote(str)
}

// ExeSuffix implements Renderer.
func (unixRenderer) ExeSuffix() string {
	return ""
}

// Mkdir implements Renderer.
func (ur unixRenderer) Mkdir(dirname string) []string {
	dirname = ur.Quote(dirname)
	return []string{
		fmt.Sprintf("mkdir %s", dirname),
	}
}

// MkdirAll implements Renderer.
func (ur unixRenderer) MkdirAll(dirname string) []string {
	dirname = ur.Quote(dirname)
	return []string{
		fmt.Sprintf("mkdir -p %s", dirname),
	}
}

// Chmod implements Renderer.
func (ur unixRenderer) Chmod(path string, perm os.FileMode) []string {
	path = ur.Quote(path)
	return []string{
		fmt.Sprintf("chmod %04o %s", perm, path),
	}
}

// Chown implements Renderer.
func (ur unixRenderer) Chown(path, owner, group string) []string {
	path = ur.Quote(path)
	return []string{
		fmt.Sprintf("chown %s:%s %s", owner, group, path),
	}
}

// Touch implements Renderer.
func (ur unixRenderer) Touch(path string, timestamp *time.Time) []string {
	path = ur.Quote(path)
	var opt string
	if timestamp != nil {
		opt = timestamp.Format("-t 200601021504.05 ")
	}
	return []string{
		fmt.Sprintf("touch %s%s", opt, path),
	}
}

// WriteFile implements Renderer.
func (ur unixRenderer) WriteFile(filename string, data []byte) []string {
	filename = ur.Quote(filename)
	return []string{
		// An alternate approach would be to use printf.
		fmt.Sprintf("cat > %s << 'EOF'\n%s\nEOF", filename, data),
	}
}

func (unixRenderer) outFD(name string) (int, bool) {
	fd, ok := ResolveFD(name)
	if !ok || fd <= 0 {
		return -1, false
	}
	return fd, true
}

// RedirectFD implements OutputRenderer.
func (ur unixRenderer) RedirectFD(dst, src string) []string {
	dstFD, ok := ur.outFD(dst)
	if !ok {
		return nil
	}
	srcFD, ok := ur.outFD(src)
	if !ok {
		return nil
	}
	return []string{
		fmt.Sprintf("exec %d>&%d", srcFD, dstFD),
	}
}

// RedirectOutput implements OutputRenderer.
func (ur unixRenderer) RedirectOutput(filename string) []string {
	filename = ur.Quote(filename)

	return []string{
		"exec >> " + filename,
	}
}

// RedirectOutputReset implements OutputRenderer.
func (ur unixRenderer) RedirectOutputReset(filename string) []string {
	filename = ur.Quote(filename)

	return []string{
		"exec > " + filename,
	}
}

// ScriptFilename implements ScriptWriter.
func (ur *unixRenderer) ScriptFilename(name, dirname string) string {
	return ur.Join(dirname, name+".sh")
}

// ScriptPermissions implements ScriptWriter.
func (ur *unixRenderer) ScriptPermissions() os.FileMode {
	return 0755
}