File: aliasfile.go

package info (click to toggle)
golang-github-juju-cmd 3.0.14-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 424 kB
  • sloc: makefile: 7
file content (53 lines) | stat: -rw-r--r-- 1,420 bytes parent folder | download
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
// Copyright 2015 Canonical Ltd.
// Licensed under the LGPLv3, see LICENSE file for details.

package cmd

import (
	"io/ioutil"
	"strings"
)

// ParseAliasFile will read the specified file and convert
// the content to a map of names to the command line arguments
// they relate to.  The function will always return a valid map, even
// if it is empty.
func ParseAliasFile(aliasFilename string) map[string][]string {
	result := map[string][]string{}
	if aliasFilename == "" {
		return result
	}

	content, err := ioutil.ReadFile(aliasFilename)
	if err != nil {
		logger.Tracef("unable to read alias file %q: %s", aliasFilename, err)
		return result
	}

	lines := strings.Split(string(content), "\n")
	for i, line := range lines {
		line = strings.TrimSpace(line)
		if line == "" || strings.HasPrefix(line, "#") {
			// skip blank lines and comments
			continue
		}
		parts := strings.SplitN(line, "=", 2)
		if len(parts) != 2 {
			logger.Warningf("line %d bad in alias file: %s", i+1, line)
			continue
		}
		name, value := strings.TrimSpace(parts[0]), strings.TrimSpace(parts[1])
		if name == "" {
			logger.Warningf("line %d missing alias name in alias file: %s", i+1, line)
			continue
		}
		if value == "" {
			logger.Warningf("line %d missing alias value in alias file: %s", i+1, line)
			continue
		}

		logger.Tracef("setting alias %q=%q", name, value)
		result[name] = strings.Fields(value)
	}
	return result
}