File: spec_file_generator.go

package info (click to toggle)
golang-github-docker-go-plugins-helpers 0.20211224-3
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie
  • size: 280 kB
  • sloc: makefile: 29
file content (58 lines) | stat: -rw-r--r-- 1,476 bytes parent folder | download | duplicates (3)
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
package sdk

import (
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"
)

type protocol string

const (
	protoTCP       protocol = "tcp"
	protoNamedPipe protocol = "npipe"
)

// PluginSpecDir returns plugin spec dir in relation to daemon root directory.
func PluginSpecDir(daemonRoot string) string {
	return ([]string{filepath.Join(daemonRoot, "plugins")})[0]
}

// WindowsDefaultDaemonRootDir returns default data directory of docker daemon on Windows.
func WindowsDefaultDaemonRootDir() string {
	return filepath.Join(os.Getenv("programdata"), "docker")
}

func createPluginSpecDirWindows(name, address, daemonRoot string) (string, error) {
	_, err := os.Stat(daemonRoot)
	if os.IsNotExist(err) {
		return "", fmt.Errorf("Deamon root directory must already exist: %s", err)
	}

	pluginSpecDir := PluginSpecDir(daemonRoot)

	if err := windowsCreateDirectoryWithACL(pluginSpecDir); err != nil {
		return "", err
	}
	return pluginSpecDir, nil
}

func createPluginSpecDirUnix(name, address string) (string, error) {
	pluginSpecDir := PluginSpecDir("/etc/docker")
	if err := os.MkdirAll(pluginSpecDir, 0755); err != nil {
		return "", err
	}
	return pluginSpecDir, nil
}

func writeSpecFile(name, address, pluginSpecDir string, proto protocol) (string, error) {
	specFileDir := filepath.Join(pluginSpecDir, name+".spec")

	url := string(proto) + "://" + address
	if err := ioutil.WriteFile(specFileDir, []byte(url), 0644); err != nil {
		return "", err
	}

	return specFileDir, nil
}