Skip to content
This repository has been archived by the owner on Oct 3, 2023. It is now read-only.

Commit

Permalink
Moved lsb_release info to package
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasdille committed Jun 12, 2023
1 parent fc7b6a2 commit 769b3a8
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
18 changes: 4 additions & 14 deletions cmd/docker-setup/cron.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package main

import (
"bufio"
"fmt"
"os"
"regexp"
"strings"

"github.com/spf13/cobra"

myos "github.com/nicholasdille/docker-setup/pkg/os"
)

var cronUpdateScript = `#!/bin/bash
Expand Down Expand Up @@ -56,18 +55,9 @@ var cronCmd = &cobra.Command{
}

func createCron() error {
f, err := os.Open(prefix + "/etc/os-release")
osVendor, err := myos.GetOsVendor(prefix)
if err != nil {
return fmt.Errorf("cannot read /etc/os-release: %w", err)
}
defer f.Close()

var osVendor string
s := bufio.NewScanner(f)
for s.Scan() {
if m := regexp.MustCompile(`^ID=(.*)$`).FindStringSubmatch(s.Text()); m != nil {
osVendor = strings.Trim(m[1], `"`)
}
return fmt.Errorf("cannot determine OS: %w", err)
}

var cronWeeklyPath string
Expand Down
32 changes: 32 additions & 0 deletions pkg/os/distribution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package os

import (
"bufio"
"fmt"
"os"
"regexp"
"strings"
)

func GetOsVendor(prefix string) (string, error) {
f, err := os.Open(prefix + "/etc/os-release")
if err != nil {
return "", fmt.Errorf("cannot read /etc/os-release: %w", err)
}
defer f.Close()

var osVendor string
s := bufio.NewScanner(f)
for s.Scan() {
re, err := regexp.Compile(`^ID=(.*)$`)
if err != nil {
return "", fmt.Errorf("cannot compile regexp: %w", err)
}
m := re.FindStringSubmatch(s.Text())
if m != nil {
osVendor = strings.Trim(m[1], `"`)
}
}

return osVendor, nil
}

0 comments on commit 769b3a8

Please sign in to comment.