From 94aa52285ba317071395c928423f54edb2578891 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= <hugo@lysator.liu.se> Date: Mon, 2 Nov 2020 21:07:50 +0100 Subject: [PATCH] Run go fmt. --- dir/dir.go | 47 +++++----- fetch/fetch.go | 243 ++++++++++++++++++++++++------------------------- file/file.go | 3 +- link/link.go | 9 +- main.go | 11 +-- 5 files changed, 149 insertions(+), 164 deletions(-) diff --git a/dir/dir.go b/dir/dir.go index 8cbca4a..8d0badc 100644 --- a/dir/dir.go +++ b/dir/dir.go @@ -1,20 +1,20 @@ package dir import ( - "context" - "os" - "syscall" "bazil.org/fuse" "bazil.org/fuse/fs" + "context" "git.lysator.liu.se/hugo/foremanFS/fetch" - "git.lysator.liu.se/hugo/foremanFS/link" "git.lysator.liu.se/hugo/foremanFS/file" + "git.lysator.liu.se/hugo/foremanFS/link" + "os" + "syscall" ) -type DirRoot struct { } -type DirHostList struct { } -type DirHost struct { hostname string } -type DirHostClasses struct { hostname string } +type DirRoot struct{} +type DirHostList struct{} +type DirHost struct{ hostname string } +type DirHostClasses struct{ hostname string } func (DirRoot) Attr(ctx context.Context, a *fuse.Attr) error { a.Inode = 0 @@ -40,7 +40,7 @@ func (DirHostClasses) Attr(ctx context.Context, a *fuse.Attr) error { return nil } -func (DirRoot) Lookup(ctx context.Context, name string) (fs.Node, error) { +func (DirRoot) Lookup(ctx context.Context, name string) (fs.Node, error) { if name == "hosts" { return DirHostList{}, nil } else { @@ -48,13 +48,12 @@ func (DirRoot) Lookup(ctx context.Context, name string) (fs.Node, error) { } } - -func (DirHostList) Lookup(ctx context.Context, name string) (fs.Node, error) { +func (DirHostList) Lookup(ctx context.Context, name string) (fs.Node, error) { _, ok := fetch.Hosts[name] - if ! ok { + if !ok { return nil, syscall.ENOENT } - return DirHost{ name }, nil + return DirHost{name}, nil } func (d DirHostClasses) Lookup(ctx context.Context, name string) (fs.Node, error) { @@ -62,26 +61,28 @@ func (d DirHostClasses) Lookup(ctx context.Context, name string) (fs.Node, error for _, class := range classes { if name == class { - return link.LinkPuppetClass{ class }, nil + return link.LinkPuppetClass{class}, nil } } return nil, syscall.ENOENT } -func (d DirHost) Lookup(ctx context.Context, name string) (fs.Node, error) { +func (d DirHost) Lookup(ctx context.Context, name string) (fs.Node, error) { switch name { - case "data": return file.DataFile{ d.hostname }, nil - case "classes": return DirHostClasses{ d.hostname }, nil + case "data": + return file.DataFile{d.hostname}, nil + case "classes": + return DirHostClasses{d.hostname}, nil } return nil, syscall.ENOENT } -func (DirRoot)ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) { - return []fuse.Dirent{ {Inode: 0, Name: "hosts", Type: fuse.DT_Dir}, }, nil +func (DirRoot) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) { + return []fuse.Dirent{{Inode: 0, Name: "hosts", Type: fuse.DT_Dir}}, nil } -func (DirHostList)ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) { +func (DirHostList) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) { var dirents []fuse.Dirent for _, item := range fetch.Hosts { dirents = append(dirents, fuse.Dirent{Inode: 0, Name: item.Data.Name, Type: fuse.DT_Dir}) @@ -89,19 +90,19 @@ func (DirHostList)ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) { return dirents, nil } -func (DirHost)ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) { +func (DirHost) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) { return []fuse.Dirent{ {Inode: 0, Name: "data", Type: fuse.DT_File}, {Inode: 0, Name: "classes", Type: fuse.DT_Dir}, }, nil } -func (d DirHostClasses)ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) { +func (d DirHostClasses) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) { classes := fetch.GetClassList(fetch.Hosts[d.hostname].Data) var out []fuse.Dirent for _, class := range classes { - out = append(out, fuse.Dirent{ Inode: 0, Name: class, Type: fuse.DT_Link }) + out = append(out, fuse.Dirent{Inode: 0, Name: class, Type: fuse.DT_Link}) } return out, nil } diff --git a/fetch/fetch.go b/fetch/fetch.go index c3d54ee..b9bd078 100644 --- a/fetch/fetch.go +++ b/fetch/fetch.go @@ -10,7 +10,9 @@ import ( ) var Hosts = map[string]Host{} + const apiurl = "https://chapman.lysator.liu.se/api/" + var classes map[float64]Puppetclass var password string var client *http.Client @@ -19,81 +21,77 @@ type Host struct { Data ResultRecord } -func Init (pw string) { +func Init(pw string) { password = pw classes = map[float64]Puppetclass{} - tr := &http.Transport { + tr := &http.Transport{ TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, } - client = &http.Client { Transport: tr } + client = &http.Client{Transport: tr} - - request, err := http.NewRequest("GET", apiurl + "hosts", nil) + request, err := http.NewRequest("GET", apiurl+"hosts", nil) if err != nil { - log.Fatal(err); + log.Fatal(err) } - request.SetBasicAuth("hugo", password); + request.SetBasicAuth("hugo", password) log.Print("Getting host list") resp, err := client.Do(request) if err != nil { - log.Fatal(err); + log.Fatal(err) } defer resp.Body.Close() + // echo curl --user "hugo:$(pass lysator/hugo)" -k https://chapman.lysator.liu.se/api/hosts - - // echo curl --user "hugo:$(pass lysator/hugo)" -k https://chapman.lysator.liu.se/api/hosts - - - log.Print("Parsing host list"); - decoder := json.NewDecoder(resp.Body); + log.Print("Parsing host list") + decoder := json.NewDecoder(resp.Body) var m Message err = decoder.Decode(&m) if err != nil { - log.Fatal(err); + log.Fatal(err) } // var data []ResultRecord // data := m.Results for _, record := range m.Results { /* - if record.Owner_name == "Hugo Hörnquist" { - fmt.Printf("%v\n", record.Name) - } + if record.Owner_name == "Hugo Hörnquist" { + fmt.Printf("%v\n", record.Name) + } */ - Hosts[record.Name] = Host{ record } + Hosts[record.Name] = Host{record} } } -func GetClassList (host ResultRecord) ([]string) { +func GetClassList(host ResultRecord) []string { var record Puppetclass if val, ok := classes[host.Id]; ok { record = val } else { - url := fmt.Sprintf(apiurl + "hosts/%d/puppetclasses", int(host.Id)); - request, err := http.NewRequest("GET",url, nil) + url := fmt.Sprintf(apiurl+"hosts/%d/puppetclasses", int(host.Id)) + request, err := http.NewRequest("GET", url, nil) if err != nil { - log.Fatal(err); + log.Fatal(err) } - request.SetBasicAuth("hugo", password); + request.SetBasicAuth("hugo", password) resp, err := client.Do(request) if err != nil { - log.Fatal(err); + log.Fatal(err) } defer resp.Body.Close() - decoder := json.NewDecoder(resp.Body); + decoder := json.NewDecoder(resp.Body) var m PuppetclassMessage err = decoder.Decode(&m) if err != nil { - log.Fatal(err); + log.Fatal(err) } classes[host.Id] = m.Results @@ -107,105 +105,104 @@ func GetClassList (host ResultRecord) ([]string) { return str } - type ResultRecord struct { - Ip string // "130.236.254.139", - Ip6 string // "2001:6b0:17:f0a0::8b", - Environment_id float64 // 1, - Environment_name string // "production", - Last_report string // "2020-10-28 19:10:57 UTC", - Mac string // "1c:c1:de:03:e7:b6", + Ip string // "130.236.254.139", + Ip6 string // "2001:6b0:17:f0a0::8b", + Environment_id float64 // 1, + Environment_name string // "production", + Last_report string // "2020-10-28 19:10:57 UTC", + Mac string // "1c:c1:de:03:e7:b6", /* - "realm_id": null, - "realm_name": null, - "sp_mac": null, - "sp_ip": null, - "sp_name": null, + "realm_id": null, + "realm_name": null, + "sp_mac": null, + "sp_ip": null, + "sp_name": null, */ - Domain_id float64 // 1, - Domain_name string // "lysator.liu.se", - Architecture_id float64 // 1, - Architecture_name string // "x86_64", - Operatingsystem_id float64 // 6, - Operatingsystem_name string // "CentOS Linux 7.8.2003", + Domain_id float64 // 1, + Domain_name string // "lysator.liu.se", + Architecture_id float64 // 1, + Architecture_name string // "x86_64", + Operatingsystem_id float64 // 6, + Operatingsystem_name string // "CentOS Linux 7.8.2003", /* - "subnet_id": null, - "subnet_name": null, - "subnet6_id": null, - "subnet6_name": null, - "sp_subnet_id": null, - "ptable_id": null, - "ptable_name": null, - "medium_id": null, - "medium_name": null, - "pxe_loader": null, - "build": false, + "subnet_id": null, + "subnet_name": null, + "subnet6_id": null, + "subnet6_name": null, + "sp_subnet_id": null, + "ptable_id": null, + "ptable_name": null, + "medium_id": null, + "medium_name": null, + "pxe_loader": null, + "build": false, */ Comment string // "", /* - "disk": null, - "installed_at": null, + "disk": null, + "installed_at": null, */ - Model_id float64 // 3, + Model_id float64 // 3, Hostgroup_id float64 // 6, - Owner_id float64 // 5, - Owner_name string // "Henrik Henriksson", - Owner_type string // "User", - Enabled bool // true, - Managed bool // false, + Owner_id float64 // 5, + Owner_name string // "Henrik Henriksson", + Owner_type string // "User", + Enabled bool // true, + Managed bool // false, /* - "use_image": null, + "use_image": null, */ Image_file string // "", /* - "uuid": null, - "compute_resource_id": null, - "compute_resource_name": null, - "compute_profile_id": null, - "compute_profile_name": null, + "uuid": null, + "compute_resource_id": null, + "compute_resource_name": null, + "compute_profile_id": null, + "compute_profile_name": null, */ // capabilities []interface{} // [ "build" ], Provision_method string // "build", - Certname string // "analysator-system.lysator.liu.se", + Certname string // "analysator-system.lysator.liu.se", /* - "image_id": null, - "image_name": null, + "image_id": null, + "image_name": null, */ Created_at string // "2020-08-16 19:51:59 UTC", Updated_at string // "2020-10-28 19:11:26 UTC", /* - "last_compile": null, + "last_compile": null, */ - Global_status float64 // 0, - Global_status_label string // "OK", - Uptime_seconds float64 // 20857465, - Organization_id float64 // 1, - Organization_name string // "Lysator", - Location_id float64 // 2, - Location_name string // "Foo", - Puppet_status float64 // 0, - Model_name string // "ProLiant DL180 G6", - Configuration_status float64 // 0, - Configuration_status_label string // "No changes", - Name string // "analysator-system.lysator.liu.se", - Id float64 // 13, - Puppet_proxy_id float64 // 1, - Puppet_proxy_name string // "chapman.lysator.liu.se", - Puppet_ca_proxy_id float64 // 1, - Puppet_ca_proxy_name string // "chapman.lysator.liu.se", + Global_status float64 // 0, + Global_status_label string // "OK", + Uptime_seconds float64 // 20857465, + Organization_id float64 // 1, + Organization_name string // "Lysator", + Location_id float64 // 2, + Location_name string // "Foo", + Puppet_status float64 // 0, + Model_name string // "ProLiant DL180 G6", + Configuration_status float64 // 0, + Configuration_status_label string // "No changes", + Name string // "analysator-system.lysator.liu.se", + Id float64 // 13, + Puppet_proxy_id float64 // 1, + Puppet_proxy_name string // "chapman.lysator.liu.se", + Puppet_ca_proxy_id float64 // 1, + Puppet_ca_proxy_name string // "chapman.lysator.liu.se", /* - "puppet_proxy": { - "name": "chapman.lysator.liu.se", - "id": 1, - "url": "https://chapman.lysator.liu.se:8443" - }, - "puppet_ca_proxy": { - "name": "chapman.lysator.liu.se", - "id": 1, - "url": "https://chapman.lysator.liu.se:8443" - }, + "puppet_proxy": { + "name": "chapman.lysator.liu.se", + "id": 1, + "url": "https://chapman.lysator.liu.se:8443" + }, + "puppet_ca_proxy": { + "name": "chapman.lysator.liu.se", + "id": 1, + "url": "https://chapman.lysator.liu.se:8443" + }, */ - Hostgroup_name string // "System", + Hostgroup_name string // "System", Hostgroup_title string // "Analysator/System" } @@ -217,35 +214,34 @@ type Message struct { } type Puppetclass struct { - Profiles []struct { - Id float64 - Name string + Id float64 + Name string Created_at string Updated_at string } /* - [{"id":433,"name":"profiles::elvisp","created_at":"2020-10-27T07:57:47.102Z","updated_at":"2020-10-27T07:57:47.102Z"}, - {"id":243,"name":"profiles::service","created_at":"2020-08-16T15:07:23.431Z","updated_at":"2020-08-16T15:07:23.431Z"}, - {"id":434,"name":"profiles::vitellary","created_at":"2020 -10-27T07:57:47.130Z","updated_at":"2020-10-27T07:57:47.130Z"}]} - */ + [{"id":433,"name":"profiles::elvisp","created_at":"2020-10-27T07:57:47.102Z","updated_at":"2020-10-27T07:57:47.102Z"}, + {"id":243,"name":"profiles::service","created_at":"2020-08-16T15:07:23.431Z","updated_at":"2020-08-16T15:07:23.431Z"}, + {"id":434,"name":"profiles::vitellary","created_at":"2020 -10-27T07:57:47.130Z","updated_at":"2020-10-27T07:57:47.130Z"}]} + */ /* - { - "total": 3, - "subtotal": 3, - "page": 1, - "per_page": 50, - "search": null, - "sort": { - "by": null, - "order": null - }, - "results": {"profiles":[{"id":433,"name":"profiles::elvisp","created_at":"2020-10-27T07:57:47.102Z","updated_at":"2020-10-27T07:57:47.102Z"},{"id":243,"name" - :"profiles::service","created_at":"2020-08-16T15:07:23.431Z","updated_at":"2020-08-16T15:07:23.431Z"},{"id":434,"name":"profiles::vitellary","created_at":"2020 - -10-27T07:57:47.130Z","updated_at":"2020-10-27T07:57:47.130Z"}]} - } + { + "total": 3, + "subtotal": 3, + "page": 1, + "per_page": 50, + "search": null, + "sort": { + "by": null, + "order": null + }, + "results": {"profiles":[{"id":433,"name":"profiles::elvisp","created_at":"2020-10-27T07:57:47.102Z","updated_at":"2020-10-27T07:57:47.102Z"},{"id":243,"name" + :"profiles::service","created_at":"2020-08-16T15:07:23.431Z","updated_at":"2020-08-16T15:07:23.431Z"},{"id":434,"name":"profiles::vitellary","created_at":"2020 + -10-27T07:57:47.130Z","updated_at":"2020-10-27T07:57:47.130Z"}]} + } */ } @@ -253,4 +249,3 @@ type PuppetclassMessage struct { Total, Subtotal, Page/*, Per_page*/ float64 Results Puppetclass } - diff --git a/file/file.go b/file/file.go index 6f767b1..0415fd4 100644 --- a/file/file.go +++ b/file/file.go @@ -1,8 +1,8 @@ package file import ( - "context" "bazil.org/fuse" + "context" "fmt" "git.lysator.liu.se/hugo/foremanFS/fetch" ) @@ -12,7 +12,6 @@ type DataFile struct { Hostname string } - func (f DataFile) Attr(ctx context.Context, a *fuse.Attr) error { a.Inode = 0 a.Mode = 0o444 diff --git a/link/link.go b/link/link.go index 1f3670f..af17d5d 100644 --- a/link/link.go +++ b/link/link.go @@ -1,20 +1,15 @@ package link import ( + "bazil.org/fuse" "context" "os" - "bazil.org/fuse" ) -type LinkPuppetClass struct { Classname string } +type LinkPuppetClass struct{ Classname string } func (LinkPuppetClass) Attr(ctx context.Context, a *fuse.Attr) error { a.Inode = 0 a.Mode = os.ModeSymlink | 0o444 return nil } - - - - - diff --git a/main.go b/main.go index 4ae44d8..0571b55 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,6 @@ package main import ( - "log" "os" _ "strings" @@ -9,13 +8,11 @@ import ( "git.lysator.liu.se/hugo/foremanFS/dir" "git.lysator.liu.se/hugo/foremanFS/fetch" - "bazil.org/fuse" "bazil.org/fuse/fs" _ "bazil.org/fuse/fs/fstestutil" ) - var classesForHost = map[string][]string{} // const apiurl := "http://localhost:8000/hosts.json" @@ -36,7 +33,7 @@ func main() { // defer c.Unmount() defer c.Close() - log.Print("Serving filesystem"); + log.Print("Serving filesystem") err = fs.Serve(c, FS{}) if err != nil { log.Fatal(err) @@ -44,15 +41,13 @@ func main() { } - - -type FS struct{ +type FS struct { // data []ResultRecord } func (fs FS) Root() (fs.Node, error) { // return Dir{ /*fs.data*/ }, nil - return dir.DirHostList{ /*fs.data*/ }, nil + return dir.DirHostList{ /*fs.data*/ }, nil } /* -- GitLab