-
Notifications
You must be signed in to change notification settings - Fork 4
/
search.go
90 lines (74 loc) · 1.79 KB
/
search.go
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
// Copyright © 2018 Platina Systems, Inc. All rights reserved.
// Use of this source code is governed by the GPL-2 license described in the
// LICENSE file.
package search
import (
"bufio"
"fmt"
"os"
"strings"
"github.com/platinasystems/goes"
"github.com/platinasystems/goes/cmd"
"github.com/platinasystems/goes/lang"
"github.com/platinasystems/flags"
"github.com/platinasystems/parms"
"github.com/platinasystems/goes/internal/partitions"
)
type Command struct {
g *goes.Goes
}
func (c Command) String() string { return "search" }
func (c Command) Usage() string {
return "NOP"
}
func (Command) Apropos() lang.Alt {
return lang.Alt{
lang.EnUS: "NOP",
}
}
func (Command) Man() lang.Alt {
return lang.Alt{
lang.EnUS: Man,
}
}
const Man = "Search for a filesystem"
func (Command) Kind() cmd.Kind { return cmd.DontFork }
func (c *Command) Goes(g *goes.Goes) { c.g = g }
func (c Command) Main(args ...string) error {
parm, args := parms.New(args, "--file", "--label", "--hint", "--uuid", "--set")
_, args = flags.New(args, "--no-floppy", "--fs-uuid")
v := parm.ByName["--set"]
if v == "" {
v = "root"
}
if len(args) != 1 {
return fmt.Errorf("Unexpected %v\n")
}
f, err := os.Open("/proc/partitions")
if err != nil {
fmt.Printf("opening /proc/partitions: %s\n", err)
return err
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := scanner.Text()
fields := strings.Fields(line)
if len(fields) < 4 || fields[0] == "major" {
continue
}
fileName := fields[3]
sb, err := partitions.ReadSuperBlock("/dev/" + fileName)
if err == nil {
u, _ := sb.UUID()
if u.String() == args[0] {
if c.g.EnvMap == nil {
c.g.EnvMap = make(map[string]string)
}
c.g.EnvMap[v] = "/dev/" + fileName
return nil
}
}
}
return nil
}