1// Copyright 2022 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package main
6
7import (
8	"fmt"
9	"slices"
10	"strconv"
11)
12
13type argvalues struct {
14	osargs []string
15	goos   string
16	goarch string
17}
18
19type argstate struct {
20	state       argvalues
21	initialized bool
22}
23
24func (a *argstate) Merge(state argvalues) {
25	if !a.initialized {
26		a.state = state
27		a.initialized = true
28		return
29	}
30	if !slices.Equal(a.state.osargs, state.osargs) {
31		a.state.osargs = nil
32	}
33	if state.goos != a.state.goos {
34		a.state.goos = ""
35	}
36	if state.goarch != a.state.goarch {
37		a.state.goarch = ""
38	}
39}
40
41func (a *argstate) ArgsSummary() map[string]string {
42	m := make(map[string]string)
43	if len(a.state.osargs) != 0 {
44		m["argc"] = strconv.Itoa(len(a.state.osargs))
45		for k, a := range a.state.osargs {
46			m[fmt.Sprintf("argv%d", k)] = a
47		}
48	}
49	if a.state.goos != "" {
50		m["GOOS"] = a.state.goos
51	}
52	if a.state.goarch != "" {
53		m["GOARCH"] = a.state.goarch
54	}
55	return m
56}
57