0

Hi I've generated Md5 and uuid in golang but now I want generate it for multiple files using command line arguments, so what exactly I've to do. This is how I've generated my md5 and uuid:

package main

import (
        "crypto/rand"
        "crypto/md5"
        "fmt"
        "io"
        "os"
        "log"
        "text/template"
       )

type Data struct {
    Uuid string
    Md5  string
}

func main() {

    uuid, err := newUUID()
        if err != nil {
            fmt.Printf("error: %v\n", err)
        }

    fmt.Printf("UUID:   %s\n", uuid)

        md5 := Getmd5(uuid)

        fmt.Printf("Checksum:   %s\n",md5)

        fillData := Data{uuid, md5}
    file, err := os.Create("text.txt")
            if err != nil {
                return
            }
            defer file.Close()

    templ, err := template.ParseFiles("template.html")
        if err !=nil{
            log.Fatalln(err)
        }
    err = templ.Execute(file,fillData)
        if err != nil{
            log.Fatalln(err)
        }
}

// newUUID generates a random UUID according to RFC 4122
func newUUID() (string, error) {
uuid := make([]byte, 16)
          n, err := io.ReadFull(rand.Reader, uuid)
          if n != len(uuid) || err != nil {
              return "", err
          }
      // variant bits
      uuid[8] = uuid[8]&^0xc0 | 0x80
          // version 4 (pseudo-random)
          uuid[6] = uuid[6]&^0xf0 | 0x40
          return fmt.Sprintf("%x-%x-%x-%x-%x", uuid[0:4], uuid[4:6], uuid[6:8], uuid[8:10], uuid[10:]), nil
}

func Getmd5(uuid string) (string) {
data := []byte(uuid)
          //md5_buffer := fmt.Sprintf("%x", md5.Sum(data))
          md5_buffer := md5.Sum(data)
          return fmt.Sprintf("{0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x};\n",md5_buffer[0:1],
                  md5_buffer[1:2],md5_buffer[2:3],md5_buffer[3:4],md5_buffer[4:5],md5_buffer[5:6],md5_buffer[6:7],md5_buffer[7:8],
                  md5_buffer[8:9],md5_buffer[9:10],md5_buffer[10:11],md5_buffer[11:12],md5_buffer[12:13],md5_buffer[13:14],md5_buffer[14:15],
                  md5_buffer[15:16])

}

Can anyone help me out?

1 Answers1

0

You can use os.Args to accept command line arguements

os.Args provides access to raw command-line arguments. Note that the first value in this slice is the path to the program, and os.Args[1:] holds the arguments to the program.

Your program will look like this, have a look at createFile and getNumberOfFiles functions and the main

package main

import (
    "crypto/md5"
    "crypto/rand"
    "errors"
    "fmt"
    "io"
    "log"
    "os"
    "strconv"
    "text/template"
)

type Data struct {
    Uuid string
    Md5  string
}

func createFile(uuid string) {
    md5 := Getmd5(uuid)
    fmt.Printf("Checksum:   %s\n", md5)
    fillData := Data{uuid, md5}
    file, err := os.Create(uuid + ".txt")
    if err != nil {
        return
    }
    defer file.Close()

    templ, err := template.ParseFiles("template.html")
    if err != nil {
        log.Fatalln(err)
    }
    err = templ.Execute(file, fillData)
    if err != nil {
        log.Fatalln(err)
    }
}

func getNumberOfFiles() (num int, err error) {
    if len(os.Args) == 1 {
        return 0, errors.New("Not enough arguements")
    }
    if num, err = strconv.Atoi(os.Args[1]); err != nil {
        return
    }
    return num, nil
}

func main() {
    numberOfFiles, err := getNumberOfFiles()
    if err != nil {
        fmt.Println(err.Error())
    }
    fmt.Printf("Creating %d files", numberOfFiles)
    for i := 0; i < numberOfFiles; i++ {
        uuid, err := newUUID()
        if err != nil {
            fmt.Printf("error: %v\n", err)
        }
        createFile(uuid)
    }
}

// newUUID generates a random UUID according to RFC 4122
func newUUID() (string, error) {
    uuid := make([]byte, 16)
    n, err := io.ReadFull(rand.Reader, uuid)
    if n != len(uuid) || err != nil {
        return "", err
    }
    // variant bits
    uuid[8] = uuid[8]&^0xc0 | 0x80
    // version 4 (pseudo-random)
    uuid[6] = uuid[6]&^0xf0 | 0x40
    return fmt.Sprintf("%x-%x-%x-%x-%x", uuid[0:4], uuid[4:6], uuid[6:8], uuid[8:10], uuid[10:]), nil
}

func Getmd5(uuid string) string {
    data := []byte(uuid)
    //md5_buffer := fmt.Sprintf("%x", md5.Sum(data))
    md5_buffer := md5.Sum(data)
    return fmt.Sprintf("{0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x,0x%x};\n", md5_buffer[0:1],
        md5_buffer[1:2], md5_buffer[2:3], md5_buffer[3:4], md5_buffer[4:5], md5_buffer[5:6], md5_buffer[6:7], md5_buffer[7:8],
        md5_buffer[8:9], md5_buffer[9:10], md5_buffer[10:11], md5_buffer[11:12], md5_buffer[12:13], md5_buffer[13:14], md5_buffer[14:15],
        md5_buffer[15:16])

}
  • Thanks @Sarathsp this clears the idea how to use command line argument but how it will be helpful to generate multiple file.? – Shriram Katneshwarkar Dec 28 '16 at 13:01
  • I am not clear about multiple file ? Now you can run the script for any files and get md5 sum of the file. Is it not what you need ? If not what exactly do you need ? – Sarath Sadasivan Pillai Dec 28 '16 at 13:06
  • actually when run my script it will create one file and in that it will generate md5 and uuid, and now I've to generate multiple files to generate multiple md5 and uuid. Means when I give command line argument as e.g. 5 then is should create 5 files with all different md5 and uuid. – Shriram Katneshwarkar Dec 28 '16 at 13:26
  • @ShriramKatneshwarkar All what you need to change would be copy all your code from the `main` as given in the question to a new function that accept a file name . In the main you will have a for loop that loops n times as the arguement and you may create `UUID` there and pass it to the function which whill now create files with file name as the uuid and will conatain your desired data – Sarath Sadasivan Pillai Dec 28 '16 at 13:31
  • @ShriramKatneshwarkar updated the answer with your code updated for your needs. Please accept the answer if this helped you – Sarath Sadasivan Pillai Dec 28 '16 at 13:43
  • @ShriramKatneshwarkar good to know, please upvote the answer if it helped you :D – Sarath Sadasivan Pillai Dec 28 '16 at 14:17