Skip to content

Commit

Permalink
feat: Improve status cmd output
Browse files Browse the repository at this point in the history
  • Loading branch information
matbme authored Apr 13, 2024
1 parent 1da22ca commit d99eeb9
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 53 deletions.
107 changes: 88 additions & 19 deletions cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func status(cmd *cobra.Command, args []string) error {

tarHeader := &tar.Header{
Name: "status.json",
Mode: 0644,
Mode: 0o644,
Size: int64(len(b)),
}
err = tarWriter.WriteHeader(tarHeader)
Expand All @@ -196,7 +196,7 @@ func status(cmd *cobra.Command, args []string) error {
}
tarHeader := &tar.Header{
Name: filepath.Join("logs", relPath),
Mode: 0644,
Mode: 0o644,
Size: info.Size(),
}
err = tarWriter.WriteHeader(tarHeader)
Expand Down Expand Up @@ -233,23 +233,92 @@ func status(cmd *cobra.Command, args []string) error {
unstagedAlert = fmt.Sprintf(abroot.Trans("status.unstagedFoundMsg"), len(pkgsUnstg))
}

agreementMsg := ""
if settings.Cnf.IPkgMngStatus == 2 {
agreementMsg = fmt.Sprintf(abroot.Trans("status.infoMsgAgreementStatus"), pkgMngAgreementStatus)
}
cmdr.Info.Printfln("%s, %s",
fmt.Sprintf(
abroot.Trans("status.infoMsg"),
present.Label, future.Label,
settings.CnfFileUsed,
specs.CPU, formattedGPU, specs.Memory,
abImage.Digest, abImage.Timestamp.Format("2006-01-02 15:04:05"), abImage.Image,
kargs,
strings.Join(pkgsAdd, ", "), strings.Join(pkgsRm, ", "), strings.Join(pkgsUnstg, ", "),
unstagedAlert,
),
agreementMsg,
)
presentMark, futureMark, err := getCurrentlyBootedPartition(a)
if err != nil {
return err
}

// ABRoot partitions:
cmdr.Bold.Println(abroot.Trans("status.partitions.title"))
cmdr.BulletList.WithItems([]cmdr.BulletListItem{
{Level: 1, Text: fmt.Sprintf(abroot.Trans("status.partitions.present"), present.Label, presentMark)},
{Level: 1, Text: fmt.Sprintf(abroot.Trans("status.partitions.future"), future.Label, futureMark)},
}).Render()

// Loaded Configuration: ...
cmdr.Bold.Print(abroot.Trans("status.loadedConfig") + " ")
cmdr.FgDefault.Println(settings.CnfFileUsed)
fmt.Println()

// Device Specification:
cmdr.Bold.Println(abroot.Trans("status.specs.title"))
cmdr.BulletList.WithItems([]cmdr.BulletListItem{
{Level: 1, Text: fmt.Sprintf(abroot.Trans("status.specs.cpu"), specs.CPU)},
{Level: 1, Text: fmt.Sprintf(abroot.Trans("status.specs.gpu"), specs.GPU)},
{Level: 1, Text: fmt.Sprintf(abroot.Trans("status.specs.memory"), specs.Memory)},
}).Render()

// ABImage:
cmdr.Bold.Println(abroot.Trans("status.abimage.title"))
cmdr.BulletList.WithItems([]cmdr.BulletListItem{
{Level: 1, Text: fmt.Sprintf(abroot.Trans("status.abimage.digest"), abImage.Digest)},
{Level: 1, Text: fmt.Sprintf(abroot.Trans("status.abimage.timestamp"), abImage.Timestamp.Format("2006-01-02 15:04:05"))},
{Level: 1, Text: fmt.Sprintf(abroot.Trans("status.abimage.image"), abImage.Image)},
}).Render()

// Kernel Arguments: ...
cmdr.Bold.Print(abroot.Trans("status.kargs") + " ")
cmdr.FgDefault.Println(kargs)
fmt.Println()

// Packages:
cmdr.Bold.Println(abroot.Trans("status.packages.title"))
cmdr.BulletList.WithItems([]cmdr.BulletListItem{
{Level: 1, Text: fmt.Sprintf(abroot.Trans("status.packages.added"), strings.Join(pkgsAdd, ", "))},
{Level: 1, Text: fmt.Sprintf(abroot.Trans("status.packages.removed"), strings.Join(pkgsRm, ", "))},
{Level: 1, Text: fmt.Sprintf(abroot.Trans("status.packages.unstaged"), strings.Join(pkgsUnstg, ", "), unstagedAlert)},
}).Render()

// Package Agreement: ...
cmdr.Bold.Print(abroot.Trans("status.agreementStatus") + " ")
cmdr.FgDefault.Println(pkgMngAgreementStatus)

return nil
}

func getCurrentlyBootedPartition(a *core.ABRootManager) (string, string, error) {
bootPart, err := a.GetBoot()
if err != nil {
return "", "", err
}
uuid := uuid.New().String()
tmpBootMount := filepath.Join("/tmp", uuid)
err = os.Mkdir(tmpBootMount, 0o755)
if err != nil {
return "", "", err
}
err = bootPart.Mount(tmpBootMount)
if err != nil {
return "", "", err
}
defer bootPart.Unmount()

g, err := core.NewGrub(bootPart)
if err != nil {
return "", "", err
}
isPresent, err := g.IsBootedIntoPresentRoot()
if err != nil {
return "", "", err
}

presentMark := ""
futureMark := ""
if isPresent {
presentMark = " ✓"
} else {
futureMark = " ✓"
}

return presentMark, futureMark, nil
}
20 changes: 10 additions & 10 deletions core/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ UUID=%s / %s defaults 0 0
varSource,
)

err := os.WriteFile(filepath.Join(rootPath, "/etc/fstab"), []byte(fstab), 0644)
err := os.WriteFile(filepath.Join(rootPath, "/etc/fstab"), []byte(fstab), 0o644)
if err != nil {
PrintVerboseErr("ABSystem.GenerateFstab", 0, err)
return err
Expand Down Expand Up @@ -347,7 +347,7 @@ func (s *ABSystem) GenerateCrypttab(rootPath string) error {
crypttabContent += fmtEntry + "\n"
}

err := os.WriteFile(rootPath+"/etc/crypttab", []byte(crypttabContent), 0644)
err := os.WriteFile(rootPath+"/etc/crypttab", []byte(crypttabContent), 0o644)
if err != nil {
PrintVerboseErr("ABSystem.GenerateCrypttab", 3, err)
return err
Expand Down Expand Up @@ -404,17 +404,17 @@ Options=%s
PrintVerboseErr("ABSystem.GenerateSystemdUnits", 0, "failed to determine escaped path", err)
return err
}
mountUnitFile := "/" + strings.Replace(string(out), "\n", "", -1) + ".mount"
mountUnitFile := "/" + strings.ReplaceAll(string(out), "\n", "") + ".mount"

err = os.WriteFile(filepath.Join(rootPath, mountUnitDir, mountUnitFile), []byte(unit), 0644)
err = os.WriteFile(filepath.Join(rootPath, mountUnitDir, mountUnitFile), []byte(unit), 0o644)
if err != nil {
PrintVerboseErr("ABSystem.GenerateSystemdUnits", 1, err)
return err
}

const targetWants string = "/local-fs.target.wants"

err = os.MkdirAll(filepath.Join(rootPath, mountUnitDir, targetWants), 0755)
err = os.MkdirAll(filepath.Join(rootPath, mountUnitDir, targetWants), 0o755)
if err != nil {
PrintVerboseErr("ABSystem.GenerateSystemdUnits", 2, err)
return err
Expand Down Expand Up @@ -642,7 +642,7 @@ func (s *ABSystem) RunOperation(operation ABSystemOperation) error {
PrintVerboseErr("ABSystemRunOperation", 4, err)
return err
}
err = os.MkdirAll(systemOld, 0755)
err = os.MkdirAll(systemOld, 0o755)
if err != nil {
PrintVerboseErr("ABSystemRunOperation", 4.1, err)
return err
Expand Down Expand Up @@ -878,7 +878,7 @@ func (s *ABSystem) RunOperation(operation ABSystemOperation) error {

uuid := uuid.New().String()
tmpBootMount := filepath.Join("/tmp", uuid)
err = os.Mkdir(tmpBootMount, 0755)
err = os.Mkdir(tmpBootMount, 0o755)
if err != nil {
PrintVerboseErr("ABSystem.RunOperation", 9, err)
return err
Expand Down Expand Up @@ -967,7 +967,7 @@ func (s *ABSystem) RunOperation(operation ABSystemOperation) error {
}

replacer := strings.NewReplacer(replacerPairs...)
os.WriteFile(grubCfgFuture, []byte(replacer.Replace(string(grubCfgContents))), 0644)
os.WriteFile(grubCfgFuture, []byte(replacer.Replace(string(grubCfgContents))), 0o644)
}

err = AtomicSwap(grubCfgCurrent, grubCfgFuture)
Expand Down Expand Up @@ -1004,7 +1004,7 @@ func (s *ABSystem) Rollback(checkOnly bool) (response ABRollbackResponse, err er

uuid := uuid.New().String()
tmpBootMount := filepath.Join("/tmp", uuid)
err = os.Mkdir(tmpBootMount, 0755)
err = os.Mkdir(tmpBootMount, 0o755)
if err != nil {
PrintVerboseErr("ABSystem.Rollback", 2, err)
return ROLLBACK_FAILED, err
Expand Down Expand Up @@ -1073,7 +1073,7 @@ func (s *ABSystem) Rollback(checkOnly bool) (response ABRollbackResponse, err er
}

replacer := strings.NewReplacer(replacerPairs...)
os.WriteFile(grubCfgFuture, []byte(replacer.Replace(string(grubCfgContents))), 0644)
os.WriteFile(grubCfgFuture, []byte(replacer.Replace(string(grubCfgContents))), 0o644)
}

err = AtomicSwap(grubCfgCurrent, grubCfgFuture)
Expand Down
46 changes: 22 additions & 24 deletions locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,30 +60,28 @@ status:
jsonFlag: "show output in JSON format"
dumpFlag: "dump the ABRoot status to an archive"
rootRequired: "You must be root to run this command."
infoMsg: |
ABRoot Partitions:
- Present: %s ◄
- Future: %s
Loaded Configuration: %s
Device Specifications:
- CPU: %s
- GPU:%s
- Memory: %s
ABImage:
- Digest: %s
- Timestamp: %s
- Image: %s
Kernel Arguments: %s
Packages:
- Added: %s
- Removed: %s
- Unstaged: %s%s
infoMsgAgreementStatus: "\nPackage agreement: %t"
partitions:
title: "ABRoot Partitions:"
present: "Present: %s%s"
future: "Future: %s%s"
loadedConfig: "Loaded Configuration:"
specs:
title: "Device Specifications:"
cpu: "CPU: %s"
gpu: "GPU: %s"
memory: "Memory: %s"
abimage:
title: "ABImage:"
digest: "Digest: %s"
timestamp: "Timestamp: %s"
image: "Image: %s"
kargs: "Kernel Arguments: %s"
packages:
title: "Packages:"
added: "Added: %s"
removed: "Removed: %s"
unstaged: "Unstaged: %s%s"
agreementStatus: "Package agreement:"
unstagedFoundMsg: "\n\t\tThere are %d unstaged packages. Please run 'abroot pkg
apply' to apply them."
dumpMsg: "Dumped ABRoot status to %s\n"
Expand Down

0 comments on commit d99eeb9

Please sign in to comment.