Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 44 additions & 2 deletions demo/disk.go
Original file line numberDiff line numberDiff line change
Expand Up@@ -21,10 +21,15 @@ var diskCommands = cli.Command{
Action: diskNewPartition,
},
{
Name: "scan",
Usage: "Scan disks on the system and dump data",
Name: "dump",
Usage: "Scan disks on the system and dump data (json)",
Action: diskScan,
},
{
Name: "show",
Usage: "Scan disks on the system and dump data (human)",
Action: diskShow,
},
},
}

Expand DownExpand Up@@ -72,6 +77,43 @@ func diskScan(c *cli.Context) error {
return nil
}

func diskShow(c *cli.Context) error {
var err error

mysys := linux.System()
matchAll := func(d disko.Disk) bool {
return true
}

if c.Args().Len() == 1 {
disk, err := mysys.ScanDisk(c.Args().First())
if err != nil {
return err
}

fmt.Printf("%s\n", disk.Details())

return nil
}

var disks disko.DiskSet
if c.Args().Len() == 0 {
disks, err = mysys.ScanAllDisks(matchAll)
} else {
disks, err = mysys.ScanDisks(matchAll, c.Args().Slice()...)
}

if err != nil {
return err
}

for _, d := range disks {
fmt.Printf("%s\n", d.Details())
}

return nil
}

func diskNewPartition(c *cli.Context) error {
mysys := linux.System()
fname := c.Args().First()
Expand Down
12 changes: 11 additions & 1 deletion disk.go
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,6 +3,7 @@ package disko
import (
"encoding/json"
"fmt"
"sort"
)

// DiskType enumerates supported disk types.
Expand DownExpand Up@@ -263,7 +264,16 @@ func (d Disk) Details() string {
lfmt := "[%2s %10s %10s %10s %-16s]\n"
buf := fmt.Sprintf(lfmt, "#", "Start", "Last", "Size", "Name")

for _, p := range d.Partitions {
pNums := make([]uint, 0, len(d.Partitions))
for n := range d.Partitions {
pNums = append(pNums, n)
}

sort.Slice(pNums, func(i, j int) bool { return pNums[i] < pNums[j] })

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, you could save yourself this dance if you used an int with IntSlice: https://golang.org/pkg/sort/#IntSlice

Not sure if you really need uint or not.

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, but I don't have an int. maybe I was being pedantic, but a partition number is an unsigned int, because it can't be < 0. (0 <= n <= 127 ... I think 127).

So I think this is about the shortest dance I can do to sort that.


for _, n := range pNums {
p := d.Partitions[n]

if fsn < len(fss) && fss[fsn].Start < p.Start {
buf += fmt.Sprintf(lfmt, "-", mbo(fss[fsn].Start), mbe(fss[fsn].Last), mbo(fss[fsn].Size()), "<free>")
fsn++
Expand Down