@@ -2,11 +2,11 @@ package main
22
33import (
44 "encoding/json"
5- "fmt"
6- "github.com/scaleway/scaleway-sdk-go/api/k8s/v1"
7- "gopkg.in/dnaeon/go-vcr.v3/cassette"
85 "log"
96 "os"
7+
8+ "github.com/scaleway/scaleway-sdk-go/api/k8s/v1"
9+ "gopkg.in/dnaeon/go-vcr.v3/cassette"
1010)
1111
1212var transientStates = map [string ]bool {
@@ -24,69 +24,82 @@ func main() {
2424 }
2525
2626 chemin := os .Args [1 ]
27+
2728 inputCassette , err := cassette .Load (chemin )
2829 if err != nil {
2930 log .Fatalf ("Erreur de lecture du fichier : %v\n " , err )
3031 }
3132
3233 outputCassette := cassette .New (chemin )
3334 transitioning := false
34- for i := 0 ; i < len (inputCassette .Interactions ); i ++ {
35+
36+ for i := range len (inputCassette .Interactions ) {
3537 interaction := inputCassette .Interactions [i ]
3638 responseBody := interaction .Response .Body
3739 requestMethod := interaction .Request .Method
3840
3941 if requestMethod != "GET" {
4042 transitioning = false
43+
4144 log .Printf ("Interaction %d is not a GET request. Recording it\n " , i )
4245 outputCassette .AddInteraction (interaction )
46+
4347 continue
4448 }
4549
4650 if responseBody == "" {
4751 log .Printf ("Interaction %d got an empty response body. Recording it\n " , i )
4852 outputCassette .AddInteraction (interaction )
53+
4954 continue
5055 }
5156
5257 var m map [string ]interface {}
58+
5359 err := json .Unmarshal ([]byte (responseBody ), & m )
5460 if err != nil {
55- fmt .Printf ("Interaction %d have an error with unmarshalling reponse body: %v. Recording it\n " , i , err )
61+ log .Printf ("Interaction %d have an error with unmarshalling response body: %v. Recording it\n " , i , err )
5662 outputCassette .AddInteraction (interaction )
63+
5764 continue
5865 }
5966
6067 if m ["status" ] == nil {
6168 log .Printf ("Interaction %d does not contain a status field. Recording it\n " , i )
6269 outputCassette .AddInteraction (interaction )
70+
6371 continue
6472 }
6573
6674 if m ["status" ] != nil {
6775 // We test if the state is transient
6876 if _ , ok := transientStates [m ["status" ].(string )]; ok {
6977 if transitioning {
70- fmt .Printf ("Interaction %d is in a transient state while we are already in transitient state. No need to record it: %s\n " , i , m ["status" ])
78+ log .Printf ("Interaction %d is in a transient state while we are already in transitient state. No need to record it: %s\n " , i , m ["status" ])
79+
7180 continue
7281 } else {
7382 transitioning = true
74- fmt .Printf ("Interaction %d is in a transient state: %s, Recording it\n " , i , m ["status" ])
83+
84+ log .Printf ("Interaction %d is in a transient state: %s, Recording it\n " , i , m ["status" ])
7585 outputCassette .AddInteraction (interaction )
7686 }
7787 } else {
7888 if transitioning {
79- fmt .Printf ("Interaction %d is not in a transient state anymore: %s, Recording it\n " , i , m ["status" ])
89+ log .Printf ("Interaction %d is not in a transient state anymore: %s, Recording it\n " , i , m ["status" ])
8090 outputCassette .AddInteraction (interaction )
91+
8192 transitioning = false
8293 } else {
83- fmt .Printf ("Interaction %d is not in a transient state: %s, Recording it\n " , i , m ["status" ])
94+ log .Printf ("Interaction %d is not in a transient state: %s, Recording it\n " , i , m ["status" ])
8495 outputCassette .AddInteraction (interaction )
8596 }
86-
8797 }
8898 }
8999 }
90100
91- outputCassette .Save ()
101+ err = outputCassette .Save ()
102+ if err != nil {
103+ log .Fatalf ("error while saving file: %v" , err )
104+ }
92105}
0 commit comments