apparmor.d/pkg/integration/tldr.go

90 lines
2.1 KiB
Go
Raw Normal View History

// apparmor.d - Full set of apparmor profiles
// Copyright (C) 2023-2024 Alexandre Pujol <alexandre@pujol.io>
// SPDX-License-Identifier: GPL-2.0-only
package integration
import (
"fmt"
"io"
"net/http"
"strings"
"github.com/arduino/go-paths-helper"
)
type Tldr struct {
Url string // Tldr download url
Dir *paths.Path // Tldr cache directory
Ignore []string // List of ignored software
}
func NewTldr(dir *paths.Path) Tldr {
return Tldr{
Url: "https://github.com/tldr-pages/tldr/archive/refs/heads/main.tar.gz",
Dir: dir,
}
}
// Download and extract the tldr pages into the cache directory
func (t Tldr) Download() error {
gzPath := t.Dir.Parent().Join("tldr.tar.gz")
if !gzPath.Exist() {
resp, err := http.Get(t.Url)
if err != nil {
return fmt.Errorf("downloading %s: %w", t.Url, err)
}
defer resp.Body.Close()
out, err := gzPath.Create()
if err != nil {
return err
}
defer out.Close()
if _, err := io.Copy(out, resp.Body); err != nil {
return err
}
}
pages := []string{"tldr-main/pages/linux", "tldr-main/pages/common"}
2024-03-10 20:07:55 +01:00
return extratTo(gzPath, t.Dir, pages)
}
// Parse the tldr pages and return a list of scenarios
func (t Tldr) Parse() (*TestSuite, error) {
testSuite := NewTestSuite()
files, _ := t.Dir.ReadDirRecursiveFiltered(nil, paths.FilterOutDirectories())
for _, path := range files {
content, err := path.ReadFile()
if err != nil {
return nil, err
}
raw := string(content)
2023-09-10 13:21:55 +02:00
t := &Test{
Name: strings.TrimSuffix(path.Base(), ".md"),
Root: false,
Arguments: map[string]string{},
2023-09-10 13:21:55 +02:00
Commands: []Command{},
}
if strings.Contains(raw, "sudo") {
2023-09-10 13:21:55 +02:00
t.Root = true
}
rawTests := strings.Split(raw, "\n-")[1:]
for _, test := range rawTests {
res := strings.Split(test, "\n")
dsc := strings.ReplaceAll(strings.Trim(res[0], " "), ":", "")
cmd := strings.Trim(strings.Trim(res[2], "`"), " ")
2023-09-10 13:21:55 +02:00
if t.Root {
cmd = strings.ReplaceAll(cmd, "sudo ", "")
}
2023-09-10 13:21:55 +02:00
t.Commands = append(t.Commands, Command{
Description: dsc,
2023-09-10 13:21:55 +02:00
Cmd: cmd,
})
}
2023-09-10 13:21:55 +02:00
testSuite.Tests = append(testSuite.Tests, *t)
}
return testSuite, nil
}