File: wincmd.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 (61 lines) | stat: -rw-r--r-- 1,530 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
// Copyright 2015 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.

package shell

import (
	"bytes"
	"fmt"
	"os"

	"github.com/juju/utils"
)

// WinCmdRenderer is a shell renderer for Windows cmd.exe.
type WinCmdRenderer struct {
	windowsRenderer
}

// Quote implements Renderer.
func (wcr *WinCmdRenderer) Quote(str string) string {
	return utils.WinCmdQuote(str)
}

// Chmod implements Renderer.
func (wcr *WinCmdRenderer) Chmod(path string, perm os.FileMode) []string {
	// TODO(ericsnow) Is this necessary? Should we use icacls?
	return nil
}

// WriteFile implements Renderer.
func (wcr *WinCmdRenderer) WriteFile(filename string, data []byte) []string {
	filename = wcr.Quote(filename)
	var commands []string
	for _, line := range bytes.Split(data, []byte{'\n'}) {
		cmd := fmt.Sprintf(">>%s @echo %s", filename, line)
		commands = append(commands, cmd)
	}
	return commands
}

// MkDir implements Renderer.
func (wcr *WinCmdRenderer) Mkdir(dirname string) []string {
	dirname = wcr.Quote(dirname)
	return []string{
		fmt.Sprintf(`mkdir %s`, wcr.FromSlash(dirname)),
	}
}

// MkDirAll implements Renderer.
func (wcr *WinCmdRenderer) MkdirAll(dirname string) []string {
	dirname = wcr.Quote(dirname)
	// TODO(ericsnow) Wrap in "setlocal enableextensions...endlocal"?
	return []string{
		fmt.Sprintf(`mkdir %s`, wcr.FromSlash(dirname)),
	}
}

// ScriptFilename implements ScriptWriter.
func (wcr *WinCmdRenderer) ScriptFilename(name, dirname string) string {
	return wcr.Join(dirname, name+".bat")
}