Skip to content

Commit

Permalink
sbctl/status: refactor to cleaner code
Browse files Browse the repository at this point in the history
Signed-off-by: Morten Linderud <[email protected]>
  • Loading branch information
Foxboron committed Jun 13, 2021
1 parent 0a0dc36 commit d8af3d8
Showing 1 changed file with 44 additions and 20 deletions.
64 changes: 44 additions & 20 deletions cmd/sbctl/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,44 +16,68 @@ var statusCmd = &cobra.Command{
RunE: RunStatus,
}

func RunStatus(cmd *cobra.Command, args []string) error {
ret := map[string]interface{}{}
if _, err := os.Stat("/sys/firmware/efi/efivars"); os.IsNotExist(err) {
return fmt.Errorf("system is not booted with UEFI")
type Status struct {
Installed bool `json:"installed"`
GUID string `json:"guid"`
SetupMode bool `json:"setup_mode"`
SecureBoot bool `json:"secure_boot"`
}

func NewStatus() *Status {
return &Status{
Installed: false,
GUID: "",
SetupMode: false,
SecureBoot: false,
}
}

func PrintStatus(s *Status) {
logging.Print("Installed:\t")
if sbctl.CheckSbctlInstallation(sbctl.DatabasePath) {
if s.Installed {
logging.Ok("Sbctl is installed")
u, err := sbctl.GetGUID()
if err != nil {
return err
}
logging.Print("Owner GUID:\t")
logging.Println(u.String())
ret["Owner GUID"] = u.String()
ret["Installed"] = true
logging.Println(s.GUID)
} else {
logging.NotOk("Sbctl is not installed")
ret["Installed"] = false
}
logging.Print("Setup Mode:\t")
if efi.GetSetupMode() {
if s.SetupMode {
logging.NotOk("Enabled")
ret["Setup Mode"] = true
} else {
logging.Ok("Disabled")
ret["Setup Mode"] = false
}
logging.Print("Secure Boot:\t")
if efi.GetSecureBoot() {
if s.SecureBoot {
logging.Ok("Enabled")
ret["Secure Boot"] = true
} else {
logging.NotOk("Disabled")
ret["Secure Boot"] = false
}
}

func RunStatus(cmd *cobra.Command, args []string) error {
stat := NewStatus()
if _, err := os.Stat("/sys/firmware/efi/efivars"); os.IsNotExist(err) {
return fmt.Errorf("system is not booted with UEFI")
}
if sbctl.CheckSbctlInstallation(sbctl.DatabasePath) {
stat.Installed = true
u, err := sbctl.GetGUID()
if err != nil {
return err
}
stat.GUID = u.String()
}
if efi.GetSetupMode() {
stat.SetupMode = true
}
if efi.GetSecureBoot() {
stat.SecureBoot = true
}
if cmdOptions.JsonOutput {
JsonOut(ret)
JsonOut(stat)
} else {
PrintStatus(stat)
}
return nil
}
Expand Down

0 comments on commit d8af3d8

Please sign in to comment.