123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- /*
- Copyright 2018 Harvey OS Team
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions are met:
- 1. Redistributions of source code must retain the above copyright notice,
- this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright notice,
- this list of conditions and the following disclaimer in the documentation
- and/or other materials provided with the distribution.
- 3. Neither the name of the copyright holder nor the names of its contributors
- may be used to endorse or promote products derived from this software
- without specific prior written permission.
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
- THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
- CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
- EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
- OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
- OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
- ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
- package main
- import (
- "encoding/json"
- "flag"
- "io/ioutil"
- "log"
- "os"
- "path/filepath"
- )
- var config = struct {
- Harvey string
- Args []string
- Except map[string]bool
- CmdName string
- FullPath string
- Src string
- Uroot string
- Cwd string
- Bbsh string
- Goroot string
- Gosrcroot string
- Arch string
- Goos string
- Gopath string
- TempDir string
- Go string
- Debug bool
- Fail bool
- }{
- Harvey: os.Getenv("HARVEY"),
- Except: map[string]bool{"sysconf.json": true},
- }
- type fixup func(string, map[string]interface{})
- func cflags(n string, jsmap map[string]interface{}) {
- if _, ok := jsmap["Cflags"]; ok {
- log.Printf("Deleting Cflags from %v", n)
- delete(jsmap, "Cflags")
- // TODO: once we have another ARCH, use it.
- a := []string{"/amd64/include/cflags.json"}
- switch tval := jsmap["Include"].(type) {
- case []interface{}:
- for _, v := range tval {
- a = append(a, v.(string))
- }
- }
- jsmap["Include"] = a
- }
- }
- func removeempty(n string, jsmap map[string]interface{}) {
- for key, val := range jsmap {
- switch tval := val.(type) {
- case map[string]interface{}:
- log.Printf("%s: tval %s", n, tval)
- if len(tval) == 0 {
- delete(jsmap, key)
- }
- case []interface{}:
- if len(tval) == 0 {
- delete(jsmap, key)
- }
- }
- }
- }
- func checkname(n string, jsmap map[string]interface{}) {
- if _, ok := jsmap["Name"]; !ok {
- log.Printf("File %v has no \"Name\" key", n)
- }
- }
- func one(n string, f ...fixup) error {
- buf, err := ioutil.ReadFile(n)
- if err != nil {
- return err
- }
- var jsmap map[string]interface{}
- if err := json.Unmarshal(buf, &jsmap); err != nil {
- return err
- }
- if config.Debug {
- log.Printf("%v: %v", n, jsmap)
- }
- mapname := jsmap["Name"].(string)
- delete(jsmap, "Name")
- var nmap = make(map[string]map[string]interface{})
- nmap[mapname] = jsmap
- buf, err = json.MarshalIndent(nmap, "", "\t")
- if err != nil {
- return err
- }
- buf = append(buf, '\n')
- if config.Debug {
- os.Stdout.Write(buf)
- } else {
- ioutil.WriteFile(n, buf, 0666)
- }
- return nil
- }
- func init() {
- if config.Harvey == "" {
- log.Fatalf("Please set $HARVEY")
- }
- if len(config.Args) == 0 {
- config.Args = []string{config.Harvey}
- }
- }
- func main() {
- var err error
- flag.BoolVar(&config.Debug, "d", true, "Enable debug prints")
- flag.Parse()
- config.Args = flag.Args()
- for _, n := range config.Args {
- err = filepath.Walk(n, func(name string, fi os.FileInfo, err error) error {
- if err != nil {
- return err
- }
- if fi.IsDir() {
- return nil
- }
- n := fi.Name()
- if len(n) < 5 || n[len(n)-5:] != ".json" {
- return nil
- }
- if config.Except[fi.Name()] {
- return nil
- }
- todo := []fixup{removeempty, checkname}
- log.Printf("process %s", name)
- err = one(name, todo...)
- if err != nil {
- log.Printf("%s: %s\n", name, err)
- return err
- }
- return nil
- })
- if err != nil {
- log.Fatalf("%v", err)
- }
- }
- }
|