File: env.go

package info (click to toggle)
golang-github-tredoe-osutil 1.5.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 476 kB
  • sloc: makefile: 4
file content (342 lines) | stat: -rw-r--r-- 7,760 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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// Copyright 2013 Jonas mg
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

// Package env implements the setting of persistent environment variables.
//
// The environment variables must be named with only English capital letters and
// underscore signs (_).
package env

import (
	"bytes"
	"errors"
	"fmt"
	"os"
	"path"

	"github.com/tredoe/osutil/config/shconf"
	"github.com/tredoe/osutil/file"
	"github.com/tredoe/osutil/user"
)

var _IS_ROOT bool

func init() {
	if os.Getuid() == 0 {
		_IS_ROOT = true
	}
}

// == Errors

var ErrNoRoot = errors.New("you have to be Root")

// NoShellError represents an account without shell.
type NoShellError string

func (e NoShellError) Error() string {
	return "shell not found for user: " + string(e)
}

// NoHomeError represents an account without home directory.
type NoHomeError string

func (e NoHomeError) Error() string {
	return "home directory not found for user: " + string(e)
}

var errKey = errors.New("environment variables must use only English capital" +
	" letters and underscore signs")

// ===

// Families of shell commands.
var (
	shFamily  = []string{"bash", "ksh", "sh"}
	cshFamily = []string{"tcsh", "csh"}
)

// shellType represents a type of shell.
type shellType int

const (
	_SHELL_SH shellType = 1 + iota
	_SHELL_CSH
)

// == Settings for each Shell family
//

var systemFile = []string{
	_SHELL_SH: "/etc/environment",
	//_SHELL_CSH: "",
}

var userFile = []string{
	_SHELL_SH: ".pam_environment",
	//_SHELL_CSH: "",
}

// To set environment variables that affect your whole session, KDE will execute
// any script it finds in '$HOME/.kde/env' whose filename ends in '.sh', and it
// will maintain all the environment variables set by them.
var kdeFile = ".kde/env"

// settings represents the files of configuration for an user, according to the shell.
type settings struct {
	global string // System wide
	user   string
	kde    string

	useKDE bool
}

// Settings files of the caller.
var _SETTINGS settings

// Sets the settings files of the caller.
func init() {
	var err error

	_SETTINGS, err = getSettingsForUid(os.Getuid())
	if err != nil {
		panic(err)
	}
}

// getSettingsForUid returns the settings files of the given user id.
func getSettingsForUid(id int) (settings, error) {
	u, err := user.LookupUID(id)
	if err != nil {
		return settings{}, err
	}
	if u.Shell == "" {
		return settings{}, NoShellError(u.Name)
	}
	if u.Dir == "" {
		return settings{}, NoHomeError(u.Name)
	}

	shell := path.Base(u.Shell)

	_settings := settings{
		global: systemFile[_SHELL_SH],
		user:   path.Join(u.Dir, userFile[_SHELL_SH]),
		kde:    path.Join(u.Dir, kdeFile),
	}
	info, err := os.Stat(_settings.kde)
	if err == nil && info.IsDir() {
		_settings.useKDE = true
	}

	for _, v := range shFamily {
		if v == shell {
			return _settings, nil
		}
	}
	/*for _, v := range cshFamily {
		if v == shell {
			return _settings, nil
		}
	}*/

	return settings{}, fmt.Errorf("shell unsopported: %s", shell)
}

// == Set variables
//

// _Set sets the value named by the key in the given filename.
func _Set(filename, key, value string) error {
	// Check if the key is already used.
	conf, err := shconf.ParseFile(filename)
	if err != nil {
		if err != os.ErrNotExist {
			return err
		}
		println("ErrNotExist") //TODO: remove
	} else {
		if _, err = conf.Get(key); err != shconf.ErrKey {
			panic("OPS")
		}
	}

	return file.AppendString(filename, key+string(conf.Separator())+value)
}

// _MSet sets multiple values named by the keys in the given filename.
func _MSet(filename string, keys, values []string) error {
	if len(keys) != len(values) {
		return fmt.Errorf("number of keys is different to number of values")
	}

	// Check if the key is already used.
	conf, err := shconf.ParseFile(filename)
	if err != nil {
		if err != os.ErrNotExist {
			return err
		}
		println("ErrNotExist") //TODO: remove
	}

	var buf bytes.Buffer
	for i, key := range keys {
		if _, err = conf.Get(key); err != nil {
			continue // TODO: log key already set.
		}

		buf.WriteString(key)
		buf.Write(conf.Separator())
		buf.WriteString(values[i])
		buf.WriteByte('\n')
	}

	return file.Append(filename, buf.Bytes())
}

// == Set session-wide variables

// Set sets the value of the environment variable named by the key that
// affects the current user.
// It returns an error, if any.
func Set(key, value string) error {
	err := _Set(_SETTINGS.user, key, value)
	if err != nil {
		return err
	}

	if _SETTINGS.useKDE {
		return _Set(_SETTINGS.kde, key, value)
	}
	return nil
}

// MSet sets multiple values of the environment variables named by the keys
// that affects the current user.
// It returns an error, if any.
func MSet(keys, values []string) error {
	err := _MSet(_SETTINGS.user, keys, values)
	if err != nil {
		return err
	}

	if _SETTINGS.useKDE {
		return _MSet(_SETTINGS.kde, keys, values)
	}
	return nil
}

// SetForUid sets the value of the environment variable named by the key that
// affects a particular user.
// It returns an error, if any.
func SetForUid(id int, key, value string) error {
	_settings, err := getSettingsForUid(id)
	if err != nil {
		return err
	}

	if err = _Set(_settings.user, key, value); err != nil {
		return err
	}
	if _settings.useKDE {
		return _Set(_settings.kde, key, value)
	}
	return nil
}

// MSetForUid sets multiple values of the environment variables named by the
// keys that affects a particular user.
// It returns an error, if any.
func MSetForUid(id int, keys, values []string) error {
	_settings, err := getSettingsForUid(id)
	if err != nil {
		return err
	}

	if err = _MSet(_settings.user, keys, values); err != nil {
		return err
	}
	if _settings.useKDE {
		return _MSet(_settings.kde, keys, values)
	}
	return nil
}

// == Set system-wide variables

// Setsys sets the value of the environment variable named by the key that
// affects the system as a whole. You must be Root.
// It returns an error, if any.
func Setsys(key, value string) error {
	if !_IS_ROOT {
		return ErrNoRoot
	}
	return _Set(_SETTINGS.global, key, value)
}

// MSetsys sets multiple values of the environment variables named by the keys
// that affects the system as a whole. You must be Root.
// It returns an error, if any.
func MSetsys(keys, values []string) error {
	if !_IS_ROOT {
		return ErrNoRoot
	}
	return _MSet(_SETTINGS.global, keys, values)
}

// SetsysForUid sets the value of the environment variable named by the key that
// affects the system as a whole. You must be Root.
// It returns an error, if any.
func SetsysForUid(id int, key, value string) error {
	if !_IS_ROOT {
		return ErrNoRoot
	}

	_settings, err := getSettingsForUid(id)
	if err != nil {
		return err
	}
	return _Set(_settings.global, key, value)
}

// MSetsysForUid sets multiple values of the environment variables named by the
// keys that affects the system as a whole. You must be Root.
// It returns an error, if any.
func MSetsysForUid(id int, keys, values []string) error {
	if !_IS_ROOT {
		return ErrNoRoot
	}

	_settings, err := getSettingsForUid(id)
	if err != nil {
		return err
	}
	return _MSet(_settings.global, keys, values)
}

// == Unset variables
//

// _Unset unsets the key in the given filename.
/*func _Unset(filename, key string) error {

}*/

// == Utility
//

// It is a common practice to name all environment variables with only English
// capital letters and underscore (_) signs.

// checkKey reports whether the key uses English capital letters and underscore
// signs.
func checkKey(s string) {
	for _, char := range s {
		if (char < 'A' || char > 'Z') && char != '_' {
			panic(errKey)
		}
	}
}