Skip to content

Instantly share code, notes, and snippets.

@owulveryck
Last active February 13, 2022 08:42
Show Gist options
  • Save owulveryck/ae042077a74102f5bf61e3b1cdff24b4 to your computer and use it in GitHub Desktop.
Save owulveryck/ae042077a74102f5bf61e3b1cdff24b4 to your computer and use it in GitHub Desktop.
Generateur de QR Code tous anti covid

go run . > output.png

package main
import (
"bytes"
"fmt"
_ "image/jpeg"
_ "image/png"
"io"
"log"
"os"
"text/template"
"time"
"github.com/skip2/go-qrcode"
)
func main() {
a := &Authorization{
CreateDate: myTime{time.Now()},
Name: "Doe",
FirstName: "John",
BirthDate: myTime{time.Now()},
BirthCity: "Paris",
Address: "1 rue de Paris 75000 Paris",
AuthorizationDate: myTime{time.Now()},
Purpose: "demarche",
}
err := a.Png(os.Stdout)
if err != nil {
log.Fatal(err)
}
}
type Authorization struct {
CreateDate myTime
Name string
FirstName string
BirthDate myTime
BirthCity string
Address string
AuthorizationDate myTime
/* Purpose is one of
"travail"
"sante"
"famille"
"handicap"
"judiciaire"
"missions"
"transit"
"animaux"
"courses"
"sport"
"rassemblement"
"demarche"
// source https://gitlab.inria.fr/stopcovid19/stopcovid-android/-/blob/master/stopcovid/src/main/assets/Attestations/form.json
*/
Purpose string
}
const tmpl = `Cree le: {{ .CreateDate.Date }} a {{ .CreateDate.HourMinute }};
Nom: {{ .Name }};
Prenom: {{ .FirstName }};
Naissance: {{ .BirthDate.Date }} a {{ .BirthCity }};
Adresse: {{ .Address }};
Sortie: {{ .AuthorizationDate.Date }} a {{ .AuthorizationDate.HourMinute }};
Motifs: {{ .Purpose }}`
func (a *Authorization) String() string {
var b bytes.Buffer
t := template.Must(template.New("attestation").Parse(tmpl))
err := t.Execute(&b, a)
if err != nil {
panic(err)
}
return b.String()
}
func (a *Authorization) Png(w io.Writer) error {
qrCode, err := qrcode.New(a.String(), qrcode.Medium)
if err != nil {
return err
}
return qrCode.Write(256, w)
}
type myTime struct {
time.Time
}
var months = [...]string{
"janvier",
"fevrier",
"mars",
"avri",
"mai",
"juin",
"juillet",
"aout",
"septembre",
"octobre",
"novembre",
"decembre",
}
func (m myTime) Date() string {
return fmt.Sprintf("%v %v %v", m.Format("02"), months[m.Month()-1], m.Format("2006"))
}
func (m myTime) HourMinute() string {
return m.Format("15:04")
}
package main
import (
_ "image/jpeg"
_ "image/png"
"testing"
"time"
)
func TestAuthorization_String(t *testing.T) {
const longForm = "Jan 2, 2006 at 3:04pm (MST)"
testTime, _ := time.Parse(longForm, "Feb 3, 2013 at 7:54pm (PST)")
type fields struct {
CreateDate myTime
Name string
FirstName string
BirthDate myTime
BirthCity string
Address string
AuthorizationDate myTime
Purpose string
}
tests := []struct {
name string
fields fields
want string
}{
{
"simple",
fields{
CreateDate: myTime{testTime},
BirthDate: myTime{testTime},
AuthorizationDate: myTime{testTime},
},
`Cree le: 03 fevrier 2013 a 19:54;
Nom: ;
Prenom: ;
Naissance: 03 fevrier 2013 a ;
Adresse: ;
Sortie: 03 fevrier 2013 a 19:54;
Motifs: `,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
a := &Authorization{
CreateDate: tt.fields.CreateDate,
Name: tt.fields.Name,
FirstName: tt.fields.FirstName,
BirthDate: tt.fields.BirthDate,
BirthCity: tt.fields.BirthCity,
Address: tt.fields.Address,
AuthorizationDate: tt.fields.AuthorizationDate,
Purpose: tt.fields.Purpose,
}
got := a.String()
if got != tt.want {
t.Errorf("Authorization.String() = %v, want %v", got, tt.want)
}
})
}
}
@pcm-x
Copy link

pcm-x commented Aug 19, 2021

Merci pour ta réponse, effectivement en voyant les fichiers je me demandais si on allait avoir droit à un autre QR code/autorisation de sortie à remplir en plus du pass sanitaire....
Ce qui était pas mal inquiétant !
Bonne journée

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment