summaryrefslogtreecommitdiff
path: root/patchset.Send.go
blob: d71df40f0131ed5f30899d392eb34a591f877cd3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package forgepb

// functions to import and export the protobuf
// data to and from config files

import (
	"errors"
	"strings"
	"time"

	"go.wit.com/log"
)

// makes a new patches protobuf. These are all the patches on your machine.
func NewPatches() *Patches {
	x := new(Patches)
	x.Uuid = "2679065e-c81d-4a00-aca4-03c158a834fb"
	x.Version = "v2.0.0 go.wit.com/lib/protobuf/forgepb"
	return x
}

func (f *Forge) SendPatchSet(pset *Patchset) error {
	var err error
	data, err := pset.Marshal()
	if err != nil {
		log.Info("proto.Marshal() pset(len) error", len(data), err)
		return err
	}
	now := time.Now()
	timestamp := now.Format("2006.01.02.150405") // bummer. other date doesn't work?
	cfgfile := "patchset/patchset." + timestamp + ".pb"
	log.Info("proto.Marshal() pset(len)", len(data))
	configWrite(cfgfile, data)

	return errors.New("don't know how to send yet")
}

func (f *Forge) SubmitDevelPatchSet(name string) (*Patchset, error) {
	pset, err := f.MakeDevelPatchSet(name)
	if err != nil {
		return nil, err
	}
	if err := f.submitPatchset(pset); err != nil {
		return nil, err
	}
	return pset, nil
}

func (f *Forge) submitPatchset(pset *Patchset) error {
	var url string
	url = f.forgeURL + "patchset"
	msg, err := pset.Marshal()
	if err != nil {
		log.Info("proto.Marshal() failed:", err)
		return err
	}
	log.Info("proto.Marshal() msg len", len(msg))
	body, err := f.HttpPost(url, msg)
	if err != nil {
		log.Info("httpPost() failed:", err)
		return err
	}

	newpb := NewPatches()
	if err := newpb.Unmarshal(body); err != nil {
		log.Infof("forged DID NOT SEND BACK PROTOBUF len(body)=%d %s (TODO: look for failure on cloudflare 'is human' check here)\n", len(body), body[0:100])
		log.Infof("TODO: try to identify data here len(body)=%d body[0:40]=%s\n", len(body), body[0:40])
		log.Info("BODY START:", body[0:10], string(body[0:10]))
		log.Info(string(body))
		if err := newpb.UnmarshalTEXT(body[2:]); err == nil {
			log.Info("wow, that did work. newpb.Len() =", newpb.Len())
		}
		return err
	}

	test := strings.TrimSpace(string(body))
	lines := strings.Split(test, "\n")
	count := 0
	for _, line := range lines {
		log.Info("got back:", line)
		count += 1
	}
	log.Info("TODO: FIX THIS AND SEND PROTOBUF BACK")
	log.Info("Total patches:", count)
	return nil
}