From 2645817f370e8fdf2a3eb9dbe4681cf1412950f3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= <hugo@lysator.liu.se>
Date: Tue, 3 Nov 2020 01:35:39 +0100
Subject: [PATCH] Interface based.

---
 fetch/fetch.go | 45 +++++++++++++++++++++++++++++++++++----------
 file/file.go   |  1 -
 2 files changed, 35 insertions(+), 11 deletions(-)

diff --git a/fetch/fetch.go b/fetch/fetch.go
index 971ea4d..5f698a6 100644
--- a/fetch/fetch.go
+++ b/fetch/fetch.go
@@ -26,7 +26,7 @@ var client *http.Client
 // echo curl --user "hugo:$(pass lysator/hugo)" -k https://chapman.lysator.liu.se/api/hosts
 
 type Host struct {
-	Data ResultRecord
+	Data HostRecord
 }
 
 func Init(pw string) {
@@ -41,8 +41,8 @@ func Init(pw string) {
 
 	client = &http.Client{Transport: tr}
 
-	var m Message
-	fff("hosts", &m)
+	var m HostMessage
+	fff_pages("hosts", &m)
 
 	for _, record := range m.Results {
 		Hosts[record.Name] = Host{record}
@@ -69,13 +69,19 @@ func fff (url string, message interface{}) {
 
 	decoder := json.NewDecoder(resp.Body)
 
-	err = decoder.Decode(&message)
+	err = decoder.Decode(message)
 	if err != nil {
 		log.Fatal(err)
 	}
+
+}
+
+func fff_pages(url string, message IsMessage) {
+	fff(url, &message)
+	log.Printf("message = %+v", message.GetMessage())
 }
 
-func GetClassList(host ResultRecord) []string {
+func GetClassList(host HostRecord) []string {
 	var record []Puppetclass
 
 	if val, ok := hostClasses[host.Id]; ok {
@@ -83,7 +89,7 @@ func GetClassList(host ResultRecord) []string {
 	} else {
 		url := fmt.Sprintf("hosts/%d/puppetclasses", int(host.Id))
 		var m PuppetclassMessage
-		fff(url, &m)
+		fff_pages(url, &m)
 
 		for _, values := range m.Results {
 			record = append(record, values...)
@@ -98,7 +104,7 @@ func GetClassList(host ResultRecord) []string {
 	return str
 }
 
-type ResultRecord struct {
+type HostRecord struct {
 	Ip               string  // "130.236.254.139",
 	Ip6              string  // "2001:6b0:17:f0a0::8b",
 	Environment_id   float64 // 1,
@@ -199,13 +205,28 @@ type ResultRecord struct {
 	Hostgroup_title string // "Analysator/System"
 }
 
+
+type IsMessage interface {
+	GetMessage() Message
+	// TODO
+	// MergeMessage(Message) Message
+}
+
 type Message struct {
-	Total, Subtotal, Page/*, Per_page*/ float64
-	Results []ResultRecord
+	Total, Subtotal, Page, Per_page float64
 	// search
 	// sort { by, order null }
 }
 
+type HostMessage struct {
+	Message
+	Results []HostRecord
+}
+
+func (m HostMessage) GetMessage() Message {
+	return m.Message
+}
+
 type Puppetclass struct {
 	Id         float64
 	Name       string // "profiles::localtime"
@@ -214,6 +235,10 @@ type Puppetclass struct {
 }
 
 type PuppetclassMessage struct {
-	Total, Subtotal, Page, Per_page float64
+	Message
 	Results map[string][]Puppetclass
 }
+
+func (m PuppetclassMessage) GetMessage() Message {
+	return m.Message
+}
diff --git a/file/file.go b/file/file.go
index 253245a..c2397bd 100644
--- a/file/file.go
+++ b/file/file.go
@@ -8,7 +8,6 @@ import (
 )
 
 type Data struct {
-	// record ResultRecord
 	Hostname string
 }
 
-- 
GitLab