github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/ci/deb/tempate.go (about)

     1  /*
     2   * Copyright (C) 2022 The "MysteriumNetwork/node" Authors.
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License as published by
     6   * the Free Software Foundation, either version 3 of the License, or
     7   * (at your option) any later version.
     8   *
     9   * This program is distributed in the hope that it will be useful,
    10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   * GNU General Public License for more details.
    13   *
    14   * You should have received a copy of the GNU General Public License
    15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16   */
    17  
    18  package deb
    19  
    20  import (
    21  	"fmt"
    22  	"io"
    23  	"net/http"
    24  	"os"
    25  	"strings"
    26  	"text/template"
    27  )
    28  
    29  const termsTemplate = `Template: mysterium/terms
    30  Type: text
    31  Description: You have to accept terms and conditions to install this software{{.}}
    32  
    33  Template: mysterium/accept_terms
    34  Type: boolean
    35  Description: Do you accept Terms and Conditions?
    36   In order to install this package you have to accept its terms and conditions
    37  `
    38  
    39  // TermsTemplateFile generates terms template file for DEB packages.
    40  func TermsTemplateFile(path string) error {
    41  	templ := template.Must(template.New("terms").Parse(termsTemplate))
    42  
    43  	resp, err := http.Get("https://raw.githubusercontent.com/mysteriumnetwork/terms/master/documents/TERMS_NODE_SHORT.md")
    44  	if err != nil {
    45  		return err
    46  	}
    47  
    48  	if resp.StatusCode != http.StatusOK {
    49  		return fmt.Errorf("unexpected status code: %d", resp.StatusCode)
    50  	}
    51  
    52  	defer resp.Body.Close()
    53  
    54  	terms, err := io.ReadAll(resp.Body)
    55  	if err != nil {
    56  		return err
    57  	}
    58  
    59  	f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o755)
    60  	if err != nil {
    61  		return err
    62  	}
    63  	defer f.Close()
    64  
    65  	s := strings.ReplaceAll(string(terms), "\n", "\n ")
    66  
    67  	err = templ.Execute(f, strings.ReplaceAll(s, " - ", " . "))
    68  	if err != nil {
    69  		return err
    70  	}
    71  
    72  	return nil
    73  }