github.com/arunkumar7540/cli@v6.45.0+incompatible/bin/reformat_translated_json.go (about)

     1  package main
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"os"
     8  	"path/filepath"
     9  	"strings"
    10  )
    11  
    12  type Entry struct {
    13  	ID          string `json:"id"`
    14  	Translation string `json:"translation"`
    15  }
    16  
    17  func main() {
    18  	if len(os.Args) != 2 {
    19  		fmt.Fprintf(os.Stderr, "usage: reformat_translated_json <tranlation directory>\n")
    20  		os.Exit(1)
    21  	}
    22  	directory := os.Args[1]
    23  	files, err := ioutil.ReadDir(directory)
    24  	if err != nil {
    25  		panic(err)
    26  	}
    27  	for _, file := range files {
    28  		if !strings.HasSuffix(file.Name(), ".all.json") {
    29  			continue
    30  		}
    31  		fullPath := filepath.Join(directory, file.Name())
    32  
    33  		fmt.Println("reformatting:", file.Name())
    34  
    35  		raw, err := ioutil.ReadFile(fullPath)
    36  		if err != nil {
    37  			panic(err)
    38  		}
    39  
    40  		var entries []Entry
    41  		err = json.Unmarshal(raw, &entries)
    42  		if err != nil {
    43  			panic(err)
    44  		}
    45  
    46  		rawOut, err := json.MarshalIndent(entries, "", "  ")
    47  		if err != nil {
    48  			panic(err)
    49  		}
    50  
    51  		err = ioutil.WriteFile(fullPath, rawOut, file.Mode())
    52  		if err != nil {
    53  			panic(err)
    54  		}
    55  	}
    56  }